Excellent topic and article! The code examples were very clear.
The reduce example is neat, after I re-read a few times to understand it. I think it's unnecessary syntactic sugar for building the .then() chain imperatively using a for loop as others have mentioned. I could be wrong and maybe this pattern of building a .then() chain with reduce is common in practice. But I still think it's better to be simple than smart, even if it means more lines of code. IMO, a reduce is better suited for more trivial problems like calculating a sum.
As for resolving promises in parallel, there is a simple improvement you can make to your code that will get you the contents in the right order to be printed to your console. All you need is to create a new outputArray and a counter=0 variable outside of your iteration, and as you are iterating through the fileNames, pass in the array index of the fileName, and push each promise result into the outputArray using the same index to build outputArray in the same order as the input array. Also within each promise, increment the counter variable and check for when counter=fileNames.length to know when all your promises have resolved before and when you can call console.log(outputArray).
I forgot to mention that using a counter variable in my example to determine when all promises have been resolved is actually a replacement for Promise.all() (it might even be that this is how Promise.all() is implemented under the hood).
This approach is very well explained in YDKJS's section on "Interaction" in the Asynchrony chapter. I recommend people to go through that entire section: github.com/getify/You-Dont-Know-JS...
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Excellent topic and article! The code examples were very clear.
The reduce example is neat, after I re-read a few times to understand it. I think it's unnecessary syntactic sugar for building the .then() chain imperatively using a for loop as others have mentioned. I could be wrong and maybe this pattern of building a .then() chain with reduce is common in practice. But I still think it's better to be simple than smart, even if it means more lines of code. IMO, a reduce is better suited for more trivial problems like calculating a sum.
As for resolving promises in parallel, there is a simple improvement you can make to your code that will get you the contents in the right order to be printed to your console. All you need is to create a new outputArray and a counter=0 variable outside of your iteration, and as you are iterating through the fileNames, pass in the array index of the fileName, and push each promise result into the outputArray using the same index to build outputArray in the same order as the input array. Also within each promise, increment the counter variable and check for when counter=fileNames.length to know when all your promises have resolved before and when you can call console.log(outputArray).
Thank you @z2lai interesting point. I will make a follow up post on this one
I forgot to mention that using a counter variable in my example to determine when all promises have been resolved is actually a replacement for Promise.all() (it might even be that this is how Promise.all() is implemented under the hood).
This approach is very well explained in YDKJS's section on "Interaction" in the Asynchrony chapter. I recommend people to go through that entire section: github.com/getify/You-Dont-Know-JS...