In es6, await is used to wait for an asynchronous request of a promise. After the asynchronous operation is completed, the execution of the async function is resumed. This keyword can only be used in "async function", and the syntax is "async function(){await=returning the asynchronous promise of promise" ask}".
How to quickly get started with VUE3.0: Enter
the operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.
? The syntax is:
async function(){await=asynchronous request that returns promise}
await is an operator used to form expressions. The result of await expression depends on what it is waiting for. If it is waiting for a Promise object, wait for the Promise object to resolve, and then get the resolve value as the result of the await expression. The sync function call will not cause blocking. All blocking inside it is encapsulated in a Promise object and executed asynchronously.
There may be await expressions in the async function. When the async function is executed, if it encounters await, the execution will be suspended first. After the triggered asynchronous operation is completed, the execution of the async function will be resumed and the parsed value will be returned.
The await keyword is only valid in async functions. If you use await outside an async function, you will only get a syntax error.
The return value
returns the processing result of the Promise object. If what is being waited for is not a Promise object, the value itself is returned.
If a Promise is passed to an await operator, await will wait for the Promise to be processed normally and return its processing result.
An example is as follows:
function testAwait (x) { return new Promise(resolve => { setTimeout(() => { resolve(x); }, 2000); }); } async function helloAsync() { var x = await testAwait ("hello world"); console.log(x); } helloAsync(); // hello world
Under normal circumstances, the await command is followed by a Promise object, which can also be followed by other values, such as strings, Boolean values, numeric values and ordinary functions.