When you are writing JavaScript, callbacks are one of the most confusing concepts. Promises are the new approach to improve working with async code...
For further actions, you may consider blocking this person and/or reporting abuse
Excellent article, I only have one suggestion.
Avoid using either
.forEach
or.reduce
for sequential promise execution - at least when overwriting references to the promises.Either use plain function recursion, or use a lib like bluebird.
Bluebird has features builtin for exactly this situation, including
.mapSeries
and concurrent execution limits, like.map(fn, {concurrency: CPU_COUNT})
Example with Bluebird:
Also, another example of bluebird features,
promisify
existing callback-based libs using a common method.So to read a file w/ Promises, use:
Good point! I mainly wanted to explain how to handle it without the help of an additional tool in a still elegant solution. I think a recursive function wouldn't make it too readable. But yeah I agree that you should ideally use something that is a bit more sophisticated. Bluebird is certainly a good solution for this.
Thanks for this! Great article! :)
Thank you! :)
Fantastic article, thanks! Shared around the office
Thanks a lot! :)
THANK YOU!
Glad you enjoyed it :)
This is amazing! This is exactly what I needed, thank you. 😬