[英]Async promise not returning result
How come I do not get a value in my res
after the promise resolves? 诺言解决后,我为什么无法在自己的
res
获得价值?
My console.log looks something like this: 我的console.log看起来像这样:
=====!!USER NOT FOUND!!=====
Res: undefined
This is my function 这是我的职责
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");
};
};
});
})};
This is my caller 这是我的电话
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();
find User should return sth: 查找用户应返回某物:
async function findUser(userID, userType) {
return await new Promise((resolve, reject) => {
...
});
}
If you do await findUser() youre waiting until the async function returns sth. 如果您确实等待findUser(),则等待异步函数返回某项 。 Your async function returns undefined.
您的异步函数返回undefined。
By the way: You might not use an async function at all: 顺便说一句:您可能根本不使用异步功能:
function findUser(userId,userType){
return new Promise(...);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.