“This is the 15th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

preface

Promise is a very common concept in front-end interviews and work, on its various methods of handwritten implementation is also very market, here today to summarize the simple implementation of the basic Promise method.

The catch () method

The catch method encapsulates the THEN method and is used only to receive error messages in reject(reason).

In the then method, the onRejected function receives the error message until the onRejected function receives it. Therefore, when writing the promise chain call, the then method does not send the onRejected function. Just add a catch() at the end of the chain so that any errors that occur in the promise chain are caught by the last catch.

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

Done () method

Catch is called at the end of the promise chain to catch error messages in the chain, but errors can also occur within catch methods, so some promise implementations add the method done.

Done provides an error-free catch method and no longer returns a promise, typically used to end a promise chain.

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

Finally () method

The finally method is used for either resolve or reject, and finally argument functions are executed.

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

Promise. All () method

Promise.all receives an array of promises, returns a new promise2, and executes all promises in the array concurrently. When all promises are resolved, Promise2 is Resolved and returns all promise results in the same order as the promise array. If there is a promise in the Rejected state, the whole promise2 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++; }}); }Copy the code

Promise. Race () method

The promise. race method receives an array of promises, returns a new promisE2, and executes the promises in the array in sequence.

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

Promise. Resolve () and Promise. Reject ()

Resolve generates a Promise with the Rejected completion state. Promise.reject generates a Promise with the Rejected completion 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);
    });
}
Copy the code

conclusion

Promise has a lot of extensions, and I’m not going to show them here. They’re basically further encapsulation of the THEN method. As long as your THEN method is fine, all other methods can rely on the THEN method to implement.

~

Thanks for reading!

~

Learn interesting knowledge, meet interesting friends, shape interesting soul!

Hello everyone, I am the author of “programming Samadhi”, I am king Yi, my public account is “programming Samadhi”, welcome to pay attention, I hope you can give me more advice!