Legendary journey 1888

Async and await

A. async B. await C. await D. await

Async and await are syntax in ES7. Before async, we run as callback functions.

The async keyword defines functions that need to be processed asynchronously. Add await await to functions that need to be asynchronously converted to synchronization so that the function will execute synchronously.

My historical record

I used to think in my JS code, I want the program to run a few seconds later, and then how do I do that, AND I found several ways to do that.

Method 1

function sleep(ms){
  return new Promise((resolve) = >setTimeout(resolve,ms));
}
async function test(){
  var temple=await sleep(1000);
  console.log(1111)
  return temple
}
test();
Copy the code

Sleep executes asynchronously, using promises, and when the promise process finishes executing, Temple will continue running and then delay printing the number.

This is the proper use of promise.

Method 2

// Use the loop
function sleep(d){
  for(var t = Date.now() ; Date.now()-t <= d ;)
  {
    continue; }}Copy the code

Through the initialization time, the program is synchronized, the program is a loop, when the time is not up to that time, will continue to loop.

Methods 3

function* sleep(ms){
   yield new Promise(function(resolve,reject){
             console.log(111);
             setTimeout(resolve,ms);
        })  
}
sleep(500).next().value.then(() = >{
  console.log(11111)})Copy the code

The use of ES6, yield iteration, is similar to promise.

Methods 4

var sleep = require('sleep');
var n=10;

sleep.sleep(n) //sleep for n seconds
sleep.msleep(n) //sleep for n miliseconds
sleep.usleep(n) //sleep for n microseconds (1 second is 1000000 microseconds)
Copy the code

I was also using Node event handling and had a library for Sleep.

synchronous

function promiseFun1 () {
	return new Promise((resolve) = >{
      		setTimeout(() = >{
          		resolve(1)},2000)})}function promiseFun2 () {
	return new Promise((resolve) = >{
      		setTimeout(() = >{
          		resolve(2)},2000)})}function promiseFun3 () {
	return new Promise((resolve) = >{
      		setTimeout(() = >{
          		resolve(3)},2000)})}function promiseFun4 () {
	return new Promise((resolve) = >{
      		setTimeout(() = >{
          		resolve(4)},1000)})}async function test() {
	const r1 = await promiseFun1(); 
    const r2 = await promiseFun2(); 
    console.log(r1,r2);
   	promiseFun3().then(console.log);
   	promiseFun4().then(console.log);
}
test()
Copy the code

The browser presses CTRL +shift+ I to run the code. The output is 1, 2,4,3. R1 and R2 wait for asynchronous functions to complete and then run the following programs, while promiseFun3 and promiseFun4 are asynchronous functions to be executed asynchronously. After the execution, the promiseFun4 functions are printed first.

In simple terms, to complete a function of the program and to complete the data, then set await synchronization, multiple asynchronous functions are executed together. After the function of the program is finished, but the function contained in the program may execute more than one function asynchronously, so you do not need to await the operation.