在承诺解决后,我如何在我的res
中得不到价值?异步承诺不返回结果
我的console.log看起来是这样的:
=====!!USER NOT FOUND!!=====
Res: undefined
这是我的功能
async function findUser(userID, userType) {
await new Promise((resolve, reject) => {
usersTable.findOne (
{ _id: userID }
,function (err, data) {
if (err) {
throw new Error('findUser: ' + err);
} else {
if (!data) {
console.log("=====!!USER NOT FOUND!!=====")
resolve("NEW");
} else {
console.log("=====USER FOUND=====")
resolve("OK");
};
};
});
})};
这是我的来电显示
async function main() {
var res = "";
try {
// Find the user
res = await findUser(userEmail, "tenant");
console.log("Res: " + res);
if (res == "NEW") {
res = await newUser(); // Add the new tenant
}
}
catch(err) {
console.error(err);
console.log(" newBooking: " + err);
callback({ error:true, err });
}
}
main();
您应该拒绝,而不是抛出一个错误 –
@Jonasw是的,我看到了,我只在开发过程扔错了,一旦工作,我拒绝。 'catch(err)'得到'reject(“错误信息”),但我不明白为什么解决不了。 – torbenrudgaard