Simple implementation of Promise basic method

Posted by webguync on Wed, 02 Feb 2022 03:49:49 +0100

preface

Promise is a very common concept in front-end interview and work. There is also a market for the handwritten implementation of its various methods. Today, I will summarize the simple implementation of promise's basic methods.

catch() method

The catch method is an encapsulation of the then method and is only used to receive error messages in reject(reason).

Because the onRejected parameter in the then method can not be passed. If not, the error information will be passed back in turn until the onRejected function receives it. Therefore, when writing the promise chain call, the then method does not pass the onRejected function, and only needs to add a catch() at the end, In this way, the error of promise in the chain will be caught by the last catch.

catch(onRejected) {
    return this.then(null, onRejected);
}

done() method

Catch is called at the end of the promise chain call to capture the error information in the chain, but errors may also occur inside the catch method, so a method done is added to some promise implementations.

done is equivalent to providing an error free catch method and no longer returning a promise. It is generally used to end a promise chain.

done() {
    this.catch(reason => {
        console.log('done', reason);
        throw reason;
    });
}

finally() method

The finally method is used for both resolve and reject, and the parameter function of finally will be executed.

finally(fn) {
    return this.then(value => {
        fn();
        return value;
    }, reason => {
        fn();
        throw reason;
    });
};

Promise.all() method

Promise.all method receives a promise array and returns a new promise 2. All promises in the array are executed concurrently. When all promises are in resolved status, promise 2 is in resolved status and returns all promise results. The order of the results is consistent with that of the promise array. If one of the projects is in the rejected state, the whole project 2 enters the rejected state.

static all(promiseList) {
    return new Promise((resolve, reject) => {
        const result = [];
        let i = 0;
        for (const p of promiseList) {
            p.then(value => {
                result[i] = value;
                if (result.length === promiseList.length) {
                    resolve(result);
                }
            }, reject);
            i++;
        }
    });
}

Promise.race() method

Promise. The race method receives a promise array, returns a new promise 2, and executes the promises in the array in sequence. A promise state is determined, and the promise 2 state is determined, which is consistent with the promise state.

static race(promiseList) {
    return new Promise((resolve, reject) => {
        for (const p of promiseList) {
            p.then((value) => {
                resolve(value);
            }, reject);
        }
    });
}

Promise.resolve() and promise reject()

Promise.resolve is used to generate a promise in the rejected completed state Reject is used to generate a promise in the rejected failed state.

static resolve(value) {
    let promise;

    promise = new Promise((resolve, reject) => {
        this.resolvePromise(promise, value, resolve, reject);
    });

    return promise;
}

static reject(reason) {
    return new Promise((resolve, reject) => {
        reject(reason);
    });
}

summary

These are basically the common methods. Promise has many extension methods, which will not be shown here one by one. They are basically the further encapsulation of the then method. As long as your then method is OK, other methods can rely on the then method.

~

~End of this article, thank you for reading!

~

Learn interesting knowledge, make interesting friends and shape interesting souls!

Hello, I'm Programming samadhi Hermit Wang, my official account is " Programming samadhi "Welcome to pay attention and hope you can give us more advice!

Topics: Javascript Front-end Promise