foreword
Promise is an extremely common concept in front-end interviews and work, and the handwritten implementation of its various methods is also very marketable. Today, I will summarize the simple implementation of the basic methods of Promise.
catch() method
catch
method is the encapsulation of the then
method, and is only used to receive the error information in reject(reason)
.
Because the onRejected
parameter can not be passed in the then
method. If it is not passed, the error information will be passed in sequence until the onRejected
function is received. Therefore, when writing the promise
chain call, the then
method does not pass the onRejected
function. Just add a catch()
at the end, so that the errors that occur at 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 there may be errors inside the catch
method, so some promise
implementations add a method done
.
done
is equivalent to providing an error-free catch
method, and no longer returns a promise
, generally used to end a promise
chain.
done() {
this.catch(reason => {
console.log('done', reason);
throw reason;
});
}
finally() method
finally
method is used for either resolve
or reject
, the parameter function of finall
y 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, returns a new promise2
, the entire array of concurrent execution of promise
, all promise
state are resolved
time, promise2
state resolved
and return all promise
results, and the results of the sequence promise
same array sequentially. If there is one promise
in the rejected
state, the entire promise2
goes into 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.race
method receives an array of promise
, returns a new promise2
, and executes the promise
in the array sequentially. There is a state of promise
determined, and the state of promise2
is determined, and it is consistent with the state of this promise
.
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 061fd3f79ed400 with a rejected
completion state, and promise
is used to generate a Promise.reject
promise
a rejected
failure 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);
});
}
Summarize
The commonly used methods are basically these. There are many extension methods for Promise
, which are not shown here. They are basically further encapsulation of the then
method. As long as your then
method has no problem, other methods can be implemented by relying on then
method.
~
~ This article is over, thanks for reading!
~
Learn interesting knowledge, meet interesting friends, and shape interesting souls!
Hello everyone, I am , the author of 〖 Programming Samadhi 〗. My is " Programming Samadhi ".
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。