因此,我目前正在尝试使用下面的代码修改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