这就是我被困的地方:
var verifyEmp = async function () {
return 'Verified';
}
Employee.find(email, password)
.then((emp) => {
console.log(emp);
return verifyEmp();
})
.then((msg) => {
console.log({ verificationMsg: msg });
})
.catch((err) => {
console.log(err);
})
如您所见,verifyEmp是一个promise
返回函数(出于演示目的,我将该函数保持尽可能简单)。因此,我想实现的目标是在第二个日志中记录{Emp:emp,verifyMsg:msg} then
。如何then
在返回的同时在第二个参数中传递emp变量promise
。
我知道这可以通过async / await方便地实现。我只是在探索如何使用传统的诺言来完成它。
如果只想使用promise,则可以将a嵌套then()
到第二个中,并根据第一个的结果进行解析:
const verifyEmp = async function () {
return 'Verified';
}
const Employee = {
async find() {
return "An employee"
}
}
Employee.find()
.then(emp => verifyEmp().then(msg => [emp, msg]))
.then(([emp, msg]) => {
/* do something with amp and msg */
console.log({emp: emp, verificationMsg: msg });
})
.catch((err) => {
console.log(err);
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句