admin管理员组文章数量:1429824
This is the code I have, I know there has been people to explaining the await functions here but I can not seem to understand why mine is not working, inside the function it console.logs the database perfectly, also on the .then section of code, but when it es to the console.log outside it won't work.
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);
// console.log(result);
});
});
}
resolveAfter1() // resolve function
.then((result)=>{console.log(result);})
.catch((error)=>{console.log(error);})
It won't show on the console.log(result) under either.
async function asyncCall() {
var result = await resolveAfter1();
return result
// console.log(result);
}
To display it under this line, what Im I doing wrong?
console.log(asyncCall(), ' why is it still pending?');
result on the console.log
Promise { <pending> } ' why is it still pending?'
This is the code I have, I know there has been people to explaining the await functions here but I can not seem to understand why mine is not working, inside the function it console.logs the database perfectly, also on the .then section of code, but when it es to the console.log outside it won't work.
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);
// console.log(result);
});
});
}
resolveAfter1() // resolve function
.then((result)=>{console.log(result);})
.catch((error)=>{console.log(error);})
It won't show on the console.log(result) under either.
async function asyncCall() {
var result = await resolveAfter1();
return result
// console.log(result);
}
To display it under this line, what Im I doing wrong?
console.log(asyncCall(), ' why is it still pending?');
result on the console.log
Promise { <pending> } ' why is it still pending?'
Share
Improve this question
asked Feb 12, 2018 at 10:39
AndrewAndrew
7052 gold badges9 silver badges26 bronze badges
3
-
2
await returns a promise, it does not wait for a value to return. You should still call
.then()
or make the callawait
in the console.log function when asking for the value. – Randy Commented Feb 12, 2018 at 10:43 -
Problem is that Im then wanting to send these values over to the client side of my game,
socket.emit('allScores', asyncCall());
– Andrew Commented Feb 12, 2018 at 10:46 -
2
@Andrew Well, you can't. An async call always returns a promise, there's no way to trick time.
await
does not cause the function to synchronously return a future value. You have to useasyncCall().then(e => socket.emit('allScores', e))
orsocket.emit('allScores', await asyncCall())
. – Bergi Commented Feb 12, 2018 at 10:54
3 Answers
Reset to default 2
asyncCall
is an async function, you have toawait
for it to resolve.
console.log(await asyncCall())
because console.log doesnt wait for the async call to finish. It already evaluates what the value of the asyncCall which therefore is a Pending Promise.
If you want to emit something because of the asyncCall then emit the value inside the asyncCall
asyncCall.then((res) => socket.emit('topic', res))
The answer to this was -
asyncCall().then((res) => socket.emit('topic', res))
Thanks for those who helped!
本文标签:
版权声明:本文标题:javascript - How can I make the console.log at the bottom wait until its all complete, then show me the answer instead of waitin 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745453329a2658986.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论