Skip to content

Instantly share code, notes, and snippets.

@CreatiCoding
Created July 27, 2018 00:32
Show Gist options
  • Save CreatiCoding/ca24ba99d8d1eaa2e1d19c611813c8cf to your computer and use it in GitHub Desktop.
Save CreatiCoding/ca24ba99d8d1eaa2e1d19c611813c8cf to your computer and use it in GitHub Desktop.
[Node.js] Promise.All with for loop
// 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);
});
@CreatiCoding
Copy link
Author

output

[10, 20, 30, 40, 50]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment