Recently, I used promise several times when I was working on a project, but I forgot most of what I learned before because I didn’t use it often. Today, I learned the content related to Promise again, and I feel I have gained a lot. Now, let’s make a simple summary.

Promise is a solution to asynchronous programming that makes more sense and is more powerful than traditional solutions — callback functions and events.

A Promise is simply a container that holds the result of an event (usually an asynchronous operation) that will end in the future. Syntactically, a Promise is an object from which to get messages for asynchronous operations.

Three states of promise

  1. Pending

  2. This is a big pity.

  3. Rejected (failed)

    Function () {let p = new Promise((resolve, resolve, resolve); Reject) => {setTimeout(() => {console.log(” chain call 1111″); Resolve (” I am data 1111″); }, 4000); }); return p; }, runAsync2() {let p = new Promise((resolve, reject) => {setTimeout(() => {console.log(” chain call 2222″)); Resolve (” I am Data 2222″); }, 2000); }); return p; }, runAsync3() {let p = new Promise((resolve, reject) => {setTimeout(() => {console.log(” chain call 3333″)); Resolve (” I am data 3333″); }, 1000); }); },

    this.runAsync1() .then((data) => { console.log(data); return this.runAsync2(); }) .then((data) => { console.log(data); return this.runAsync3(); }) .then((data) => { console.log(data, 888); }); Then chain-call is a solution to asynchronyCopy the code

    RunReject () {this.runasync4 ().then((data) => {console.log(” I am a number “+ data); }). Catch ((err) => {console.log(” I reported an error “+ err); }); }, runAsync4() { console.log(this.myNum); let p = new Promise((resolve, reject) => { setTimeout(() => { if (this.myNum > 5) { resolve(this.myNum); } else { reject(this.myNum); }}, 1000); }); return p; },

The usual AXIos chain-call then method is really the HTTP library that Promise encapsulates so we can use then!