async & await
Posted Tekkaman
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了async & await相关的知识,希望对你有一定的参考价值。
【await】
The await
operator is used to wait for a Promise
. It can only be used inside an async function
.
Returns the resolved value of the promise, or the value itself if it‘s not a Promise
.
The await
expression causes async
function execution to pause, to wait for the Promise
‘s resolution, and to resume the async
function execution when the value is resolved. It then returns the resolved value. If the value is not a Promise
, it‘s converted to a resolved Promise
.
If the Promise
is rejected, the await
expression throws the rejected value.
async function f3() { try { var z = await Promise.reject(30); } catch(e) { console.log(e); // 30 } } f3();
【async】
When an async
function is called, it returns a Promise
. When the async
function returns a value, the Promise
will be resolved with the returned value. When the async
function throws an exception or some value, the Promise
will be rejected with the thrown value.
参考:
1、https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await
2、https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
以上是关于async & await的主要内容,如果未能解决你的问题,请参考以下文章
Swift新async/await并发中利用Task防止指定代码片段执行的数据竞争(Data Race)问题