preface
This article sorted out the underlying principles of some of the Promise methods. If you have different views on the answer, you are welcome to add the discussion in the comments section. Of course, you are also welcome to point out the questions in the comments section.
1, Promise. All ()
// promise.prototype. All = function(promiseArray){}
function PromiseAll(promiseArray) {
return new Promise(function(resolve,reject){
// Determine the parameter type
if(!Array.isArray(promiseArray)){
return reject(new TypeError('arguments muse be an array'))}let counter = 0,
promiseNum = promiseArray.length,
resolvedArray = new Array(promiseNum);
for(let i = 0; i < promiseNum; i++){
Promise.resolve(promiseArray[i]).then(function(value){
counter++;
resolvedArray[i] = value;
if(counter === promiseNum){
return resolve(resolvedArray)
}
},function(reason){
return reject(reason)
})
}
})
}
// Create a test case
const createPromise =function (value,delay){
return new Promise((resolve,reject) = >{
setTimeout(() = >{
resolve(value)
},delay)
})
}
const promiseList= [createPromise(1.1000),createPromise(2.2000),createPromise(3.3000)]
PromiseAll(promiseList)
.then(res= > console.log(res)) // Print after 3 seconds ["1", "2", "3"]
.catch((err) = > console.log(err))
Copy the code
2, Promise. AllSettled ()
// Returns the results (array) of each promise, both successful and failed
const PromiseAllSettled = function (promiseArray) {
return new Promise((resolve, reject) = > {
// Promises are an array
if (!Array.isArray(promiseArray))
return reject(new TypeError('args must be an array'));
Closure, save the result array
const res = [];
/ / count
let count = 0;
for (let i = 0; i < promiseArray.length; i++) {
Promise.resolve(promiseArray[i]).then(data= > {
res[i] = data;
}).catch(err= > {
res[i] = err;
}).finally(() = > {
/ / Promise. Prototype. Finally at the end of the Promise, no matter the result is fulfilled or is rejected, will perform the specified callback function
count++;
if(count === promiseArray.length) { resolve(res); }})}})}Copy the code
3, Promise. Race ()
// race ----- promise.race ([P1, p2, p3]) returns the result that completes the fastest, regardless of whether the result itself is in a success or failure state.
Any () receives a Promise iterable that returns the first successful result. If no promises succeed (all promises fail/refuse), return a failed promise
const PromiseRace = function(promiseArray){
return new Promise((resolve,reject) = >{
if(!Array.isArray(promiseArray)) reject(new TypeError('args must be an array'));
promiseArray.forEach(promise= > {
Promise.resolve(promise)
.then((value) = >resolve(value))
.catch((error) = >reject(error))
})
})
}
Copy the code
conclusion
Feel good writing, helpful to you, you can share with people around you, the more you share knowledge, do not be stingy
Follow-up update promise related foundation and some methods of the underlying principle of implementation summary, please pay attention to me, tidy up, share with you, we learn the front end together.