These days of interview, I know promise also know async but finally people asked me the difference between them, I was confused, what is the difference, I do not know, I dare not ask, can only check down, now, understand almost, harm side learning, do not understand the place there are a lot of

1.promise

ECMAScript 6’s new reference type Promise can be instantiated with the new operator. Creating a new contract requires passing in an executor function as an argument.

Use 1.

Promise has three states, i. E. pending, SUCCESS, and Rejected.

let n = new Promise((resolve,reject) = > {
        / /...
        resolve('123')}); n.then(result= > {
        console.log(result);/ / 123
    });

Copy the code

The state transition is irrevocable regardless of whether resolve() or reject() is called. Continuing to modify the state will silently fail, as shown below:

    let p = new Promise((resolve, reject) = > {
            resolve(); 
            reject(); // No effect
           });
Copy the code

To avoid getting stuck in a pending state, add a timed exit feature. For example, you can use setTimeout to set a callback that will reject the date after 10 seconds anyway:

   let p = new Promise((resolve, reject) = > { 
           setTimeout(reject, 10000); Reject () after 10 seconds // Executes the logic of the function
         });
         
           setTimeout(console.log, 0, p); // Promise <pending>
           setTimeout(console.log, 11000, p); // Check the status after 11 seconds
Copy the code

2.Promise.prototype.finally()

Promise. Prototype. Finally add () method is used to issue about onFinally handler, the handler in the futures will perform when converted to solve or reject status. This method can avoid redundant code in the onResolved and onRejected handlers. But the onFinally handler has no way of knowing whether the date status is resolved or rejected, so this method is mostly used to add cleanup code.

3.async await

It can force the processing of an asynchronous function into a synchronous function, which is pretty cool, and it’s essentially a syntactic candy derived from promise, which is more elegant to write. Async and await are used in pairs !!!! Keep that in mind, any one less will report an error

 // no async await
async function foo () {
  await Promise.resolve().then(resolve= > {
    console.log("1");
  });
  console.log(3);
}
foo();
Copy the code
 // add async await
async function foo () {
  await Promise.resolve().then(resolve= > {
    console.log("1");
  });
  console.log(3);
}
foo();
Copy the code

3. The difference between

Async /await is built on Promises and cannot be used for normal and node callbacks

Async /await is more elegant than promise

Async /await is like Promises, it does not block

Async /await code looks like synchronous code

In fact, there is no big difference after learning, promise said async await syntax candy

Continue to work hard, continue to refueling, any mistakes can comment to tell me, I will humbly accept, ha ha ha