“This is the second day of my participation in the August More Text Challenge.

Differences from Generater

Better semantics

Async and await are semantic clearer than * and yield. Async means that there is an asynchronous operation in a function, and await means that the following expression needs to wait for the result

Built-in actuator

Generator functions must be executed by an executor, hence the CO module, while async functions have their own executor. In other words, async functions are executed exactly like normal functions, with only one line.

Wider applicability

According to the co module convention, yield can only be followed by a Thunk function or a Promise object, while await can be followed by a Promise object and a value of the original type (numeric, string, and Boolean, but automatically converted to an immediate Resolved Promise object).

The return value is Promise

Async functions return a Promise object, which is much more convenient than Generator functions returning an Iterator. You can specify what to do next using the then method.

example

AsyncPrint (” Hello World “, 1000); , met await, this represents a 99% with asynchronous code here, will cede control, continue to implement the main thread code behind the console. The log (” hello async “); When the execution stack is empty, it will check the message (task) queue (including micro task and macro task), execute the global micro task first, and execute the macro task after the completion of the micro task. Microtask await timeout(ms); New Promise((resolve) => {setTimeout(resolve, ms)}) return Promise(resolve() => {setTimeout(resolve, ms)}) Continue with the async synchronization code by console.log(value);

return new Promise((resolve) => { setTimeout(resolve, ms) }) } async function asyncPrint(value, ms) { await timeout(ms);  console.log(value); } asyncPrint("hello world", 1000); console.log("hello async"); // Output order // hello async // hello worldCopy the code

AsyncPrint (” Hello World “, 1000); , met await, this represents a 99% with asynchronous code here, will cede control, continue to implement the main thread code behind the console. The log (” hello async “); } function() {console.log(1122)}, 0); , give the macro task setTimeout to the macro task queue and continue to execute the synchronization code console.log(value) in async; , perform await 2 and put 2 directly into the microtask; Continue with the async synchronization code console.log(3344); . After the synchronization code is completed, execute the microtask, the microtask is cleared, and execute the macro task console.log(1122).

function timeout(ms) { return new Promise((resolve) => { setTimeout(resolve, ms) }) } async function asyncPrint(value, ms) { await setTimeout(function() { console.log(1122) }, 0); console.log(value); await 2; console.log(3344); } asyncPrint("hello world", 1000); console.log("hello async"); // Hello async // hello world // 3344 // 1122Copy the code

Async Precautions

  • Await the Promise object after the await command, and the result may be rejected, so it is better to await the await command in the try… In the catch block.
  • Asynchronous operations following multiple await commands, if there is no secondary (dependency) relationship, should be fired at the same time.
  • Third, the await command can only be used in async functions. If used in normal functions, an error will be reported.