`async`关键字的含义是什么? [英] What is the meaning of the `async` keyword?

查看:79
本文介绍了`async`关键字的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在node.js中阅读异步/等待.我了解到 await 关键字等待一个诺言得到解决,或者如果被拒绝则抛出异常.

I have been reading up on async/await in node.js. I have learnt that the await keyword waits for a promise to be resolved, or throws an exception if it was rejected.

我还了解到,每个要使用 await 的函数都必须标记为 async .但是,标记为异步功能意味着什么?

I have also learnt that every function that wants to use await needs to be marked async. However, what does it mean for a function to be marked async?

我能够找到的所有资源和博客文章似乎都对 await 进行了详细解释,但忽略了 async 函数的概念,或简要介绍了它.例如,这位作者就是这样说的:

All the resources and blog posts I was able to find seem to explain await in great detail, but ignore the concept of an async function, or briefly gloss over it. For instance, this author puts it like this:

这使函数隐式返回Promise.

This makes the function return a Promise implicitly.

async 关键字实际上有什么作用?函数隐式返回Promise是什么意思?除了能够使用 await 以外,还有哪些副作用?

What does the async keyword really do? What does it mean for a function to implicitly return a Promise? What are the side effects other than being able to use await?

好的,所以从到目前为止我收到的答案中可以看出,它很简单地将函数的返回值包装到 Promise 中,就像 Promise.then 一样.不过,这只是一个新问题.为什么使用 await 的函数需要进行 async 异步并因此返回 Promise ?

Alright, so from the answers I have received so far it's clear that it simply wraps the function's return value into a Promise, much like Promise.then would. That just leaves a new question though. Why does a function that uses await need to be async and thus return a Promise?

推荐答案

无论您实际从函数中什么返回,您的 async 函数仍将返回承诺.如果您返回一个数字,它实际上会返回一个 Promise ,它会解析为您最初返回的 Number .这使您可以编写同步的外观"代码.

No matter what you actually return from your function, your async function will still return a Promise. If you return a Number, it actually returns a Promise that resolves to the Number your originally returned. This allows you to write synchronous "looking" code.

不必写这个:

function foo(){
    return Promise.resolve("foo");
}

您可以这样写:

async function foo(){
    return "foo";
}

foo()将自动返回解析为"foo" Promise .

and foo() will automagically return a Promise that resolves to "foo".

针对您的评论:

它的行为是否像Promise.then那样,如果您已经返回一个Promise,它不会再次包装吗?

Does it behave like Promise.then in the sense that if you already return a Promise, it won't wrap it again?

await 将剥离Promise,直到它达到一个值:

await will peel the Promise until it gets to a value:

async function foo() {
    return Promise.resolve(Promise.resolve(true));
}

async function bar() {
    return true;
}

(async function () {
    let tmp;
    tmp = await foo();
    console.log(tmp);
    tmp = await bar();
    console.log(tmp);
    console.log("Done");
}());

/*
Prints:

true
true
Done
*/

为什么需要异步?

await 会暂停该上下文的执行,直到它正在等待的 Promise 不再挂起为止.在正常功能中不会发生这种情况.

await, just like yield in a generator, pauses the execution of that context until the Promise it's waiting on is no longer pending. This cannot happen in normal functions.

如果函数是 async ,但不包含 await ,则promise将立即得到解决,并且所有回调将在下一个滴答处运行.

If a function is async but does not contain an await, the promise will be resolved immediately, and any callbacks will be ran on the next tick.

这篇关于`async`关键字的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆