Promises and async/await is a welcomed addition to the newer versions of JavaScript. If you are not using it yet and are trapped in the callback he...
For further actions, you may consider blocking this person and/or reporting abuse
You forget about for await to run async functions one-by-one
An added bonus is that you can easily break or return based on the result of each function, if you had to.
Updated!
Hi Afif, thanks for the post, it was helpful. With help from a user on Reddit, there's another entry you might want to add, "async pools". If it's useful, please copy anything you want from this public gist: gist.github.com/jzohrab/a6701d0087...
Cheers and regards! jz
Thanks! That's interesting. Definitely faster than just processing promises one-by-one.
Thanks for great post @afif Sohaili.
I'm new to javascript but not new to programming.
I was interested in the "Complete async tasks in sequence" method using reduce as you describe.
In the spirit of "DRY", I immediately wanted to encapsulate it:
which then gives us the re-usable "sequencer" which takes as parameters:
which means we can do this:
Does this look like sound javascript?
I can't help thinking the resolveInNSeconds function could be a bit simpler?
Sorry I hadn't come and check this often enough and missed it. Yes, you can abstract those however you'd like to make it more readable, and functions are something you can toss around in JavaScript.
Thanks for this. When you write the "explainer" article for number 3, please contrast it with an old-school for loop. I'm interested to see if your opinion changes!
Hi, I just googled for it, and I was mindblown. Definitely will opt for that instead. I didn't know. 😁
This is why I love the comments section!
Edit: I just reread your comment, and 🤔 do you mean the new
for await of
that came with ES2018?I did not mean for-await-of, I mean:
Less code, less cognitive overhead!
jsfiddle.net/4umfreak/bkqcymuL/
Yeah, that's definitely a more readable approach. I have to be honest I haven't used for loops in a while so it didn't really occur to me.
@4umfreak updated the code to use plain ol' for-loops. Took me long enough.
there is a nice lib for promises
bluebirdjs.com/docs/api-reference....
Yep, definitely a great library for Promises. I just felt like most of the time what I need with Promises are already available in ES6+.
Awesome!! This has helped me a lot refactoring an endpoint in an API that acts as payments cart and was originally implemented using Promise.all. Thank you v.m