Created
July 27, 2018 00:32
-
-
Save CreatiCoding/ca24ba99d8d1eaa2e1d19c611813c8cf to your computer and use it in GitHub Desktop.
[Node.js] Promise.All with for loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// It is bad case about using Promise.All | |
let value = [1,2,3,4,5]; | |
var promises = []; | |
for(let i=0;i<5;i++){ | |
promises.push( | |
((data)=>{ | |
return new Promise((resolve, reject)=>{ | |
setTimeout(() => { | |
resolve(data*10); | |
}, 3000); | |
}); | |
})(value[i]) | |
); | |
} | |
Promise.all(promises).then((data) => { | |
console.log(data); | |
}).catch((e) => { | |
console.log(e); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output