因此,我目前正在嘗試使用下面的代碼修改promise中的全局對象,但是,當我在末尾使用console.log對象時,它會返回'undefined' 'id'的關鍵。我有點困惑,爲什麼在承諾的成功範圍內,它並沒有在患者對象內設置新的關鍵和價值。提前致謝!將Promise的對象添加到對象
patient = { first_name: first_name, last_name: last_name, gender: gender, dob: dob }
postgres.findPatient(patient)
.then(function(success){
patient.id = success.id
})
.catch(function(err){
if (err.received === 0) {
postgres.createPatient(patient)
.then(function(success){
postgres.findPatient(patient)
.then(function(success){
patient.id = success.id
})
})
.catch(function(err){
if (err) console.log(err);
})
}
})
console.log(patient) // yields
patient = {
first_name: 'billy,
last_name: 'bob',
gender: 'm',
dob: '1970-01-01' }
的[?我如何返回從一個異步調用的響應(可能的複製http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-異步調用) –
嘗試console.log對象*在末尾*,而不是在您開始異步調用之後。 – Bergi