当前位置:网站首页>Simple implementation of promise basic method

Simple implementation of promise basic method

2022-06-23 02:36:00 Programming samadhi

Promise.001

Preface

Promise It is a very common concept in front-end interview and work , The handwritten implementation of its various methods is also very marketable , Let's summarize here today Promise Simple implementation of basic methods .

catch() Method

catch The way is right then Method encapsulation , Only for receiving reject(reason) Error message in .

Because in then In the method onRejected Parameters are not transferable , Without transmission , The error message will be passed back in turn , Until there is onRejected Until the function receives , So I'm writing promise When chain calls , then The method doesn't spread onRejected function , Just add one at the end catch() That's all right. , So in the chain promise Every mistake that happens will be punished by the last catch Capture to .

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

done() Method

catch stay promise Call at the end of the chain call , Used to capture error messages in the chain , however catch Errors can also occur inside methods , So some promise A method has been added to the implementation done .

done It is equivalent to providing an error free catch Method , And don't return a promise , Usually used to end a promise chain .

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

finally() Method

finally Method for both resolve still reject , finall y All parameter functions are executed .

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

Promise.all() Method

Promise.all Method to receive a promise Array , Return to a new promise2 , Execute all of the array concurrently promise , all promise All States are resolved when , promise2 Status as resolved And return all promise result , The order of the results and promise Array order is consistent . If there is a promise by rejected state , Then the whole promise2 Get into 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.race Method to receive a promise Array , Return to a new promise2 , Execute... In the array in order promise , There is one promise Status determination , promise2 The state is determined , And with this promise In the same 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 Used to generate a rejected Perfect state promise , Promise.reject Used to generate a rejected Failure state promise .

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 the common methods , Promise There are also many ways to expand , It's not going to be shown here , It's basically right then Further encapsulation of methods , As long as yours. then There is no problem with the method , Other methods can be relied on then Method realization .

~ The end of this paper , Thank you for reading !

原网站

版权声明
本文为[Programming samadhi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202021138087018.html