[英]Async function returning before await is complete
I have two questions from the following example: 我从以下示例中得到两个问题:
Why is does x log before y? 为什么x在y之前记录? Why is xa Promise?
为什么xa承诺?
I'm expecting bar
to wait for foo
to resolve with the value 'Hello'
before it logs and then returns it. 我期待
bar
在它记录之前等待foo
以值'Hello'
解析然后返回它。
let foo = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Hello');
}, 2000);
})
}
let bar = async () => {
let y = await foo();
console.log(y);
return y;
}
let x = bar();
console.log(x);
What I'm expecting to see is 我期待看到的是
'Hello' // console.log(y)
'Hello' // console.log(x)
What I'm getting is 我得到的是
Promise {<pending>} // console.log(x)
'Hello' // console.log(y)
Shouldn't bar
wait for foo
to resolve before returning y
? 应该不是
bar
等foo
返回前解决y
?
Any help would be much appreciated! 任何帮助将非常感激!
Here is the documentation about async function
: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function 以下是有关
async function
的文档: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
And Promise
object: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise 和
Promise
对象: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
It is stated that an async function
returns a Promise
object which is in a pending
state until it is resolved. 声明
async function
返回一个Promise
对象,该对象在解析pending
处于pending
状态。
Now let's look at your code: 现在让我们来看看你的代码:
You assign to x
the return value of bar()
, which is a Promise
, since there is no await
keyword to stop the execution (And you can not add one, because you are outside of an async function
) this promise is logged as pending because the async function bar()
has not yet returned. 您为
x
分配了返回值bar()
,这是一个Promise
,因为没有await
关键字来停止执行(而且你不能添加一个,因为你在async function
)这个promise被记录为pending因为async function bar()
尚未返回。
Meanwhile, in the async function bar()
you assign to y
the return value of the async function foo()
which is also a Promise
, but this time you make it await for the result. 同时,在
async function bar()
,为y
分配async function foo()
的返回值,它也是一个Promise
,但这次你等待结果。 After 2 seconds of waiting time, the promise is fulfiled and y
is logged with the parameter of the resolve()
method, which is Hello
. 经过2秒的等待时间后,承诺得以实现,并使用
resolve()
方法的参数记录y
,即Hello
。
Thus the behaviour you witness is the expected one. 因此,您见证的行为是预期的行为。
You need to await
the function: 你需要
await
这个功能:
let example = async () => {
let foo = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Hello');
}, 2000);
})
}
let bar = async () => {
let y = await foo();
console.log(y);
return y;
}
let x = await bar();
console.log(x);
}
example();
You should use an await
before calling bar()
but, actually, you can use await
just when you wrapped it by a async
function like below: 你应该在调用
bar()
之前使用await
,但实际上,你可以使用await
就像你用async
函数包装它一样,如下所示:
async function Func() {
let foo = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Hello');
}, 2000);
})
}
let bar = async () => {
let y = await foo();
console.log(y);
return y;
}
let x = await bar();
console.log(x);
}
When you execute the Func();
当你执行
Func();
your expectation will fire. 你的期望会激发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.