I met an interesting interview question today, which asked me to implement promise.all
with js native. Since I was not familiar with this API, I only realized the function of “resolve” and “callback”
Promise. All description
promise.all (iterable) method returns a Promise instance in which all promises in the iterable parameter are “resolved” or arguments containing no Promise. If the Promise has a rejected in the parameter, the instance will call back reject because of the result of the first failed Promise
JS implements the all method
function all(iterableArr) {
//Returns a PROMISE instance (satisfying the first rule)
return new Promise((resolve,reject)=>{
//resArr is used to store all the resolve promises of the resolve
let resArr = [];
// iterate through all elements of the array.
for(let i in iterableArr){
let cur = iterableArr[i];
// If the current object is of type promises
if(typeof cur === 'object' && typeof cur.then ==='function'){
cur.then((res)=>{
resArr[i] = res;
//If all resolve, the length of the stored resArr is the same as the length of the incoming iterableArr, and the entire promise is then resolve (in accordance with the second rule).
if(resArr.length === iterableArr.length){
resolve(resArr);
}
// If the state of the current instance of promise is reject, then the entire promise will be reject. (The third rule is met.)
},reject)
}else{
resArr[i] = cur
}
}
})
}
//Test
all([
Promise.reject(100),//Promise.resolve(100)
123,
new Promise((resolve,reject)=>{
setTimeout(()=>{
resolve("DD")
},5000)
})
]).then((res)=>{
console.log(res)
}).catch((err)=>{
console.log(err)
})
The above
Read More:
- Module not found: Error: Can‘t resolve ‘core-js/modules/es.promise.js‘ in
- runtime-core.esm-bundler.js5c406620 [Vue warn] Unhandled error during execution of native event h
- [Solved] Turf.js error: uncaught (in promise) error: the solution of invalid unit
- [Solved] echarts Draw Errror: echarts-d9fd185e.js:31447 Uncaught (in promise) Error: Initialize failed: invalid dom.
- request.js?b775:101 Uncaught (in promise) Error: Failed to convert value of type ‘java.lang.String’ to required type ‘java.lang.Long’;
- How to Solve Uncaught (in promise) Error (Two Solutions)
- [Solved] Nuxt Import qrcodejs2.js / QRCode.js Error: document is not defined
- Solution to some map files in JS folder after Vue packaging (remove the map. JS file)
- VueUncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location: “/
- Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to crrent location: “/home“
- [Solved] webpack Package Error: ERROR in multi ./src/main.js ./dist/bundle.js Module not found: Error: Can‘t resolv
- [Solved] IE Browser Error: unhandled promise rejection error: access is denied. File stream Download
- Solution to build error in Vue project (error in static/JS)/vendor.xxxxx.js from UglifyJs)
- [Solved] Uncaught (in promise) Error: Avoided redundant navigation to current location:
- Vue Error: Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location
- How to solve Uncaught (in promise) error in VUE?
- React native android: How to Upload Formdata
- [Solved] Uncaught (in promise) TypeError: XXX.a is not a constructor
- [Solved] Vue Error: Error in v-on handler (Promise/async): “[object object]“
- [Solved] Uncaught (in promise) DOMException: Failed to load because no supported source was found.