Promise to realize

  1. Define the class, declare the status and values, and declare an executor. The resolve or Rejected methods are called in the executor

  2. Implementation of then methods (including the two functions accepted by THEN, and the implementation of Pending)

  3. Chain implementation

  4. Return type (Promise or normal value)

  5. Promise. Resolve/race/all/reject

class PR { static PENDING = 'pending'; static FULFILLED = 'fulfilled'; static REJECTED = 'rejected'; constructor(executor) { this.status = PR.PENDING; // Set pending this.value = null; This. Callbacks = []; // executor executor(this.resolve.bind(this), This.reject. Bind (this))} catch (e) {this.reject(e)}} // Promise. Resolve implement static resolve(value) {return new PR((resolve, reject) => { if (value instanceof PR) { value.then(resolve, reject); } else { resolve(value); }}); } // promise. reject implement static reject(reason) {return new PR((_, reject) => {reject(reason); }); } // Promise. Promise (promises) {return new Promise ((resolve, resolve); reject) => { promises.map(promise => { promise.then(value => { resolve(value); }); }); }); } // High similarity: high or low; return new PR((resolve, reject) => { promises.forEach((promise, index) => { promise.then( value => { resolves.push(value); if (resolves.length == promises.length) { resolve(resolves); } }, e => { reject(e); }); }); }); } resolve(value) { if (this.status === PR.PENDING) { this.status = PR.FULFILLED; // This. Value = value; SetTimeout (() => {this.callbacks. Map (callback => {callback.ondepressing (value)) }) }) } } reject(value) { if (this.status === PR.PENDING) { this.status = PR.REJECTED; This. value = value; SetTimeout (() => {this.callbacks. Map (callback => {callback.onRejected(value)}) // This is a big pity, which is a pity, onFulfilled) {// This is a big pity, onFulfilled) {// This is a big pity, onFulfilled) {// This is a big pity, onFulfilled) {// This is a big pity, onFulfilled) Set the default value to function if (typeof ondepressing! = 'function') { onFulfilled = value => value; } if (typeof onRejected ! = 'function') { onRejected = value => value } // 5. Let promise = new PR((resolve, (this. Status == PR.pending) {let obj = {ondepressing: value => { try { let result = onFulfilled(value) // 6. Parse (promise, result, resolve, reject); } catch (e) { onRejected(e) } }, onRejected: value => { try { let result = onRejected(value) this.parse(promise, result, resolve, reject); This.callbacks. Push (obj) {// This.status == PR. This is a big pity. SetTimeout (() => {try {let result = ondepressing (this.value)) this.parse(promise,result, resolve, reject); } catch (e) { onRejected(e); }}); If (this.status == pr.rejected) {setTimeout(() => {try {let result = onRejected(this.value)) this.parse(promise, result, resolve, reject); } catch (e) { onRejected(e); }}); } }) return promise; Parse (promise, result, resolve, reject) { if (promise == result) { throw new TypeError("Chaining cycle detected for promise"); } try { if(result instanceof PR) { result.then(resolve, reject); } else { resolve(result); } } catch (e) { reject(e); Resolve ("resolve").then(value => {console.log(value); }); let p = new PR((resolve, reject) => { resolve(1) }).then(value => { console.log(value); Return new PR((resolve, reject) => {resolve(' return promise')})}).then(value => {console.log(value)})Copy the code

Note: Please refer to the uncle of self-backing people (Jujujuzi)