当前位置:网站首页>Simple implementation of promise basic method
Simple implementation of promise basic method
2022-06-23 02:36:00 【Programming samadhi】
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 !
边栏推荐
- Markdown - enter a score (typora, latex)
- February 6, 2022: Arithmetic Sequence Division II - subsequence. Give you an integer array n
- Automatically update site statistics with actions
- Salesforce fileUpload (III) how to display uploaded images
- How to make traditional Chinese medicine labels with pictures
- Push RTMP stream using ffmpeg
- How to batch make decreasing serial number barcode
- Stop automatically after MySQL starts (unable to start)
- February 4, 2022: combined total IV. Give you a number composed of different integers
- Pywebio to quickly build web applications
猜你喜欢

Evolution history of mobile communication

Ansible practice of Nepal graph

Understand GB, gbdt and xgboost step by step

Docker installs mysql5.7 and mounts the configuration file

For Xiaobai who just learned to crawl, you can understand it after reading it

Data analysis method - user group analysis

6. template for integer and real number dichotomy

Lying in the trough, write it yourself if you can't grab it. Use code to realize a Bing Dwen Dwen. It's so beautiful
What is sitelock? What is the function?

Interviewer: with the for loop, why do you need foreach??
随机推荐
Gorilla/mux framework (RK boot): add swagger UI
How to prohibit copying and copying files to the local server remote desktop
1.3-1.4 web page data capture
Buuctf misc-[actf freshman competition 2020]outline
II Data preprocessing
Apache Druid's engineering practice in shopee
Interviewer: why does TCP shake hands three times and break up four times? Most people can't answer!
February 4, 2022: combined total IV. Give you a number composed of different integers
Salesforce fileUpload (I) how to configure the file upload function
PNAs: power spectrum shows obvious bold resting state time process in white matter
How to set up an H5 demo of easyplayer locally to play h265 video streams?
method
Detailed explanation of various networking modes of video monitoring platform
"Return index" of live broadcast E-commerce
How to make keyword targeted layout based on search sources?
What is ISBN code and how to make it
Vulnhub DC-5
JS case: support canvas electronic signature function on PC and mobile
Applet control version update best practices
The difference between script in head and body