A pointcut starts with a scenario description that defines the functionality we want to implement

Executor function

The constructor takes an executor self-executing function. The resolve and reject state reversals are passed in synchronously at new. The state reversal function is called internally based on the asynchronous task execution result (success or failure), passing the state to subsequent THEN.

State transition

Promise has three states. The default is pending, which is fulfilled and failed after a failure. And once the state is changed, it cannot be changed or reversed

Then method

The then method takes two functions that represent callback functions in the success and failure states. The value of the return in the callback function is passed as an argument to the subsequent THEN. To implement the chain call.

To determine whether a variable is a promise, two conditions are required. First, it is an object, and then methods exist on the object. Therefore, the result of then implementation is also a promise

implementation

class Promise {
    constructor(executor){
        this.status = 'pending'.this.value = undefined;
        this.reason = undefined;
        this.onFulfilled =undefined;
        this.onRejected = undefined;
        this.onFulfilledList = [];
        this.onRejectedList = [];
        try {
            executor(this.resolve, this.reject);
        } catch(e) {
            this.reject(e)
        }
    }
    resolve(value){
        if(this.status == 'pending') {
            this.status = 'fullfilled';
            this.value = value;
            this.onFulfilledList.forEach(item= > item())
        }
    }
    reject(reason){
        if(this.status == 'pending') {
            this.status = 'rejected';
            this.reason = reason;
            this.onRejectedList.forEach(item= > item())
        }
    }
    then(onFulfilled, onRejected){
        // then a new promise is returned
        let result;
        if(this.status == 'fullfilled' && onFulfilled) {
            result = onFulfilled(this.value)
            return Promise.resolve(result);
        }

        if(this.status == 'rejected' && onRejected) {
            result = onRejected(this.reason);
            return Promise.resolve(result);
        }

        if(this.status == 'pending') {
            onFulfilled && this.onFulfilledList.push(() = >onFulfilled(this.value));
            onRejected && this.onRejectedList.push(() = > onRejected(this.reason)); }}}Promise.resolve = function(value) {
    if(typeof value == 'object' && value.then) {
        return value;       
    } else {
        return new Promise((resolve, reject) = >{
            resolve(value)
        })
    }
}

Promise.all = function(list) {
    return new Promise((resolve,reject) = > {
        let result = [];
        let count = 0;
        for(let i = 0; i < list.length; i++)  {
            if(typeof list[i] == 'object' && list[i].then) {
                Promise.resolve(list[i]).then(data= >{
                    result[i] = data;
                    count++;
                },reject)
            }else{ result[i] = list[i]; count++; }}if(count == list.length) { resolve(result); }})}// promise.race likewise, as long as there is a success, all resolve
// promise.finally Whether it succeeds or fails, the passed callback function is executed, and then can still be followed after execution
Copy the code