admin管理员组文章数量:1323179
So the result in the console shows as -
Promise { <pending> } ' why is it still pending?'
[ { _id: 5a7c6552380e0a299fa752d3, username: 'test', score: 44 } ]
So it tells me Promise { pending } but then follows with the answer that I wanted to see which is -
[ { _id: 5a7c6552380e0a299fa752d3, username: 'test', score: 44 } ]
but how can I fix that promise pending section, it's a console.log that I run at the bottom of the code.
function resolveAfter1() {
return new Promise((resolve, reject) => {
var scoresFromDb = db.account.find({}, { username: 1, score: 1 }).toArray(function(err, result) {
if (err)
reject(err);
else
resolve(result);
});
});
}
resolveAfter1() // resolve function
.then((result)=>{console.log(result);})
.catch((error)=>{console.log(error);})
async function asyncCall() {
var result = await resolveAfter1();
// console.log(result);
}
console.log(asyncCall(), ' why is it still pending?');
So the result in the console shows as -
Promise { <pending> } ' why is it still pending?'
[ { _id: 5a7c6552380e0a299fa752d3, username: 'test', score: 44 } ]
So it tells me Promise { pending } but then follows with the answer that I wanted to see which is -
[ { _id: 5a7c6552380e0a299fa752d3, username: 'test', score: 44 } ]
but how can I fix that promise pending section, it's a console.log that I run at the bottom of the code.
function resolveAfter1() {
return new Promise((resolve, reject) => {
var scoresFromDb = db.account.find({}, { username: 1, score: 1 }).toArray(function(err, result) {
if (err)
reject(err);
else
resolve(result);
});
});
}
resolveAfter1() // resolve function
.then((result)=>{console.log(result);})
.catch((error)=>{console.log(error);})
async function asyncCall() {
var result = await resolveAfter1();
// console.log(result);
}
console.log(asyncCall(), ' why is it still pending?');
Share
Improve this question
asked Feb 9, 2018 at 14:52
AndrewAndrew
7052 gold badges9 silver badges26 bronze badges
3
-
1
Because you don't wait for
asyncCall
withawait
. – lolbas Commented Feb 9, 2018 at 14:55 - What do you remend, I should use instead? – Andrew Commented Feb 9, 2018 at 14:56
- @Andrew First port of call ► async function && await || How to “await” for a callback to return? – Nope Commented Feb 9, 2018 at 14:58
2 Answers
Reset to default 4Replace:
console.log(asyncCall(), ' why is it still pending?');
With:
async function run() {
console.log(await asyncCall());
}
run();
You were printing the result of asyncCall
which is an async function
. Async functions wrap their returned result in a Promise
, and if you want the actual value which the promise resolved to, you have to use await someAsyncFunc()
.
Put in a simple example:
async function asyncCall() {
return 1;
}
async function run() {
console.log(asyncCall()); // this doesn't wait for the Promise to resolve
console.log(await asyncCall()); // this does
}
run();
Because you are console.log a AsyncFunction directly without await, it will return you a unresolved Promise object
function resolveAfter1() {
return new Promise((resolve, reject) => {
setTimeout(resolve('a'), 100)
})
}
async function asyncCall() {
var result = await resolveAfter1();
return result
}
(async () => {
console.log(await asyncCall(), ' It is not pending anymore!!!!')
})()
本文标签: javascriptPromiseltpendinggt39 why is it still pending39 How can I fix thisStack Overflow
版权声明:本文标题:javascript - Promise { <pending> } ' why is it still pending?' How can I fix this? - Stack Overflo 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742131046a2422159.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论