[英]Function returns before await finishes executing
I am trying to await
for a database query to finish before returning a function, but my approach seems not to work for some reason. 我正在尝试
await
数据库查询完成,然后再返回函数,但是由于某种原因,我的方法似乎无法正常工作。
It seems that the function returns before the await
returns the resolved Promise
. 似乎该函数在
await
返回已解决的Promise
之前返回。
var returnAfterReading = async () => {
let myEntityJSONConst = {
myEntity : {}
}
myEntityJSONConst["myEntity"] = await dbQueryPromise();
return myEntityJSONConst;
}
console.log(JSON.stringify(returnAfterReading())) // {"myEntity":{}}
What am I doing wrong? 我究竟做错了什么?
Thank you all in advance. 谢谢大家。
You're missing await before calling returnAfterReading()
function: 您在调用
returnAfterReading()
函数之前缺少等待:
Please note the
returnAfterReading()
function should be called withinasync
context (inasync
function) or else you need to use promise (egreturnAfterReading().then(data => { /* bla-bla */ })
)请注意,
returnAfterReading()
函数应在async
上下文中(在async
函数中)调用,否则您需要使用promise(例如returnAfterReading().then(data => { /* bla-bla */ })
))
const returnAfterReading = async () => {
const myEntityJSONConst = {
myEntity : {}
}
myEntityJSONConst["myEntity"] = await dbQueryPromise();
return myEntityJSONConst;
}
// ... In some other async context
console.log(JSON.stringify(await returnAfterReading()));
It seems that the function returns before the await returns the resolved Promise.
似乎该函数在等待返回已解决的Promise之前返回。
Yes, that is exactly how async
functions work. 是的,这正是
async
功能的工作方式。 When executing an async
function, the moment it hits an await
that is awaiting a promise, it immediately returns from the async
function. 当执行
async
函数时,当它碰到正在等待诺言的await
,它将立即从async
函数中返回。 And, the return value is a promise that has not yet been resolved. 而且,返回值是尚未解决的承诺。 So, ALL
async
functions return a promise. 因此,所有
async
函数都返回一个Promise。 The caller of your function will fairly quickly get back a promise (that is not yet resolved). 函数的调用者将很快获得一个诺言(尚未解决)。 The caller will need to use
await
(if within a higher level async
function) or .then()
on that promise to get the eventual resolved value. 调用方将需要在该承诺上使用
await
(如果在更高级别的async
函数中)或.then()
来获取最终的解析值。
Once the promise that is being awaited
inside the async
function resolves, the function will continue executing. async
函数中awaited
的诺言解析后,该函数将继续执行。 If it hits another await
, it will again pause execution. 如果遇到另一个
await
,它将再次暂停执行。 Whatever the eventual return value is from the function's code will then signal the original promise that was returned to be resolved with that value and the caller can then get access to that value via their own await
or .then()
on the returned promise. 无论最终从函数代码中返回的值是什么,都将用信号通知要返回的原始承诺,并使用该值对其进行解析,然后调用者可以通过自己的
await
或返回的诺言上的.then()
来访问该值。
So, assuming that dbQueryPromise();
因此,假设
dbQueryPromise();
does actually return a promise that is resolved with its proper value, then you just need to either use await
or .then()
on the return value from calling returnAfterReading()
实际上没有返回与它应有的价值解决的承诺,那么你只需要使用任何
await
或.then()
从调用的返回值returnAfterReading()
const returnAfterReading = async () => {
let myEntityJSONConst = {
myEntity : {}
}
myEntityJSONConst.myEntity = await dbQueryPromise();
return myEntityJSONConst;
}
returnAfterReading().then(val => {
console.log(val);
}).catch(err => {
console.log(err);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.