Looping Over Async Calls by Emulating .reduce() with async/await

Variables & Functions used in the code:

const fakeAPI = (delay, value) =>
 new Promise(resolve => setTimeout(() => resolve(value), delay));

const useResult = x => console.log(new Date(), x);

This code prints what forEach cant with async calls by using a reduce() implementation instead:

const forEachAsync = (arr, fn) =>
 arr.reduce(
 (promise, value) => promise.then(() => fn(value)),
 Promise.resolve()
 );
(async () => {
 console.log("START FOREACH VIA REDUCE");
 await forEachAsync([1, 2, 3, 4], async n => {
 const x = await fakeAPI(n * 1000, n);
 useResult(x);
 });
 console.log("END FOREACH VIA REDUCE");
})();

OUTPUT

/*
START FOREACH VIA REDUCE
2019-10-13T20:02:23.437Z 1
2019-10-13T20:02:24.446Z 2
2019-10-13T20:02:25.949Z 3
2019-10-13T20:02:27.952Z 4
END FOREACH VIA REDUCE
*/

What is the purpose of promise and value parameter in the arr.reduce function in the context of main code block?

Is value parameter passed to the async n arrow function?

Even though, I would prefer the explanation because I am studying functional-prog, but, if there is any other way of handling this in a FP manner, I would be appreciated.

1,498 thoughts on “Looping Over Async Calls by Emulating .reduce() with async/await”

  1. It’s the best time to make a few plans for the long run and
    it is time to be happy. I have learn this put up and if I may I desire to counsel you
    few interesting things or advice. Perhaps you could write
    next articles referring to this article. I wish to read even more things approximately
    it!

    Reply
  2. Someone essentially lend a hand to make significantly posts I might state.

    That is the very first time I frequented your website page and to this point?
    I amazed with the research you made to create this particular submit extraordinary.
    Great job!

    Reply
  3. I have been browsing on-line greater than three hours as of late, but I never found any interesting article like yours.
    It is pretty price enough for me. In my view, if all web owners and bloggers made just right content as you did,
    the web will likely be much more useful than ever before.

    Reply
  4. I’ll immediately seize your rss as I can’t in finding
    your e-mail subscription hyperlink or e-newsletter service.
    Do you’ve any? Kindly allow me recognize so that I may
    just subscribe. Thanks.

    Reply
  5. Pingback: viagra cheap
  6. It is appropriate time to make a few plans for the long run and it’s time to be happy.
    I have learn this post and if I could I desire to counsel you few interesting issues or suggestions.
    Maybe you could write subsequent articles relating to this article.
    I want to learn more issues about it!

    Reply
  7. It is the best time to make some plans for the future and it is time to be happy.
    I’ve read this submit and if I may just I desire to suggest you few attention-grabbing issues
    or tips. Perhaps you can write subsequent articles referring to this article.
    I wish to read even more things about it!

    Reply
  8. It’s the best time to make a few plans for the longer term and it is time to be
    happy. I have read this submit and if I could I desire to counsel you some interesting issues or suggestions.
    Perhaps you can write subsequent articles referring to this article.
    I desire to learn even more things about it!

    Reply
  9. While the secret and even the screen photographs might lead
    many to imagine that is actually a Pokemon enviornment type sport, that is not even remotely true – It’s a cell port
    of their board recreation that entails gathering figures and spinning arrows.

    Reply
  10. Greetings from Carolina! I’m bored to death at
    work so I decided to browse your blog on my iphone during lunch break.
    I really like the info you present here and can’t wait to take a look when I get home.
    I’m shocked at how quick your blog loaded on my phone ..
    I’m not even using WIFI, just 3G .. Anyhow, very
    good blog!

    Reply
  11. Admiring the persistence you put into your website and
    in depth information you provide. It’s nice to come across a
    blog every once in a while that isn’t the same unwanted rehashed information. Excellent read!
    I’ve saved your site and I’m adding your
    RSS feeds to my Google account.

    Reply
  12. I’ve been surfing on-line more than three hours as of late, but
    I by no means found any attention-grabbing article like yours.

    It is beautiful worth enough for me. In my view, if all site owners and bloggers made excellent
    content material as you probably did, the internet will likely
    be much more useful than ever before.

    Reply
  13. It is the best time to make some plans for the future
    and it’s time to be happy. I have read this post and if I could I
    want to suggest you few interesting things or advice.
    Maybe you could write next articles referring to this article.
    I want to read more things about it!

    Reply
  14. I’ve been surfing online greater than three hours as of late,
    yet I never discovered any interesting article like yours.
    It is lovely value enough for me. In my view, if all web owners and bloggers made just right
    content as you did, the internet will likely be a lot more helpful than ever before.

    Reply
  15. I have been surfing online more than 3 hours today, yet I
    never found any interesting article like yours. It
    is pretty worth enough for me. In my view, if all web owners and bloggers made good content as you did,
    the web will be much more useful than ever
    before.

    Reply
  16. Hey! Someone in my Facebook group shared this website
    with us so I came to check it out. I’m definitely enjoying the information. I’m book-marking and will be
    tweeting this to my followers! Outstanding blog and amazing design.

    Reply
  17. I will immediately clutch your rss feed as I can’t find your email subscription hyperlink
    or e-newsletter service. Do you have any?
    Please let me know so that I could subscribe. Thanks.

    Reply
  18. It is perfect time to make some plans for the future and it’s
    time to be happy. I have read this post and if
    I could I want to suggest you few interesting things or advice.
    Maybe you can write next articles referring to this article.
    I want to read even more things about it!

    Reply
  19. Hi would you mind sharing which blog platform you’re working
    with? I’m looking to start my own blog in the near future but I’m having a tough
    time deciding between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design seems different then most blogs and I’m
    looking for something unique. P.S Apologies for being off-topic but
    I had to ask!

    Reply
  20. It is appropriate time to make some plans for
    the future and it’s time to be happy. I have read this post and if
    I could I wish to suggest you some interesting things or suggestions.
    Maybe you could write next articles referring to this article.
    I want to read even more things about it!

    Reply
  21. Hi, I do believe this is an excellent blog.

    I stumbledupon it 😉 I am going to revisit yet again since i have
    saved as a favorite it. Money and freedom is the best way to
    change, may you be rich and continue to help other people.

    Reply
  22. Hey just wanted to give you a quick heads up.
    The text in your post seem to be running off the
    screen in Chrome. I’m not sure if this is a formatting issue or something to do with web browser compatibility but I thought I’d post to let you
    know. The design and style look great though! Hope you get the issue resolved soon. Kudos

    Reply
  23. I have been browsing online more than three
    hours these days, yet I by no means discovered any interesting article like yours.
    It’s beautiful price sufficient for me. Personally, if all website owners
    and bloggers made excellent content as you probably did, the
    internet will be much more useful than ever before.

    Reply
  24. Hi there, just became aware of your blog through Google,
    and found that it is really informative. I am going to watch
    out for brussels. I will be grateful if you continue this in future.
    Many people will be benefited from your writing. Cheers!

    Reply
  25. One thing is that if you are searching for a education loan you may find that you’ll want a co-signer. There are many circumstances where this is correct because you might find that you do not possess a past credit score so the loan company will require you have someone cosign the financial loan for you. Thanks for your post.

    Reply
  26. I have been browsing online more than 3 hours nowadays, yet I never
    found any fascinating article like yours. It’s lovely worth sufficient for me.
    In my opinion, if all web owners and bloggers made good content material as you did, the web will likely be much more useful than ever before.

    Reply
  27. I have been browsing online more than 2 hours today, yet I never found
    any interesting article like yours. It’s pretty worth enough
    for me. Personally, if all website owners and bloggers made
    good content as you did, the internet will be much more useful than ever before.

    Reply
  28. I really love your website.. Very nice colors & theme.
    Did you create this site yourself? Please reply back as I’m planning to create my own website
    and want to find out where you got this from or exactly what the theme is named.

    Appreciate it!

    Reply
  29. I’ve been surfing on-line greater than 3 hours as of late, but I never discovered any
    interesting article like yours. It is pretty price enough for me.
    Personally, if all web owners and bloggers made just right content material as you
    did, the net will probably be much more useful than ever before.

    Reply
  30. I will right away grab your rss as I can not to find your e-mail subscription hyperlink or e-newsletter service.
    Do you have any? Please allow me know so that I
    could subscribe. Thanks.

    Reply
  31. Howdy! This post could not be written any better!

    Reading through this post reminds me of my previous roommate!
    He continually kept talking about this. I most certainly will send this
    post to him. Pretty sure he’s going to have a great read.
    Many thanks for sharing!

    Reply
  32. Hello would you mind letting me know which webhost
    you’re using? I’ve loaded your blog in 3 different internet browsers and I must say this blog loads a lot quicker then most.
    Can you recommend a good web hosting provider at a fair price?
    Thanks, I appreciate it!

    Reply
  33. I’ve been browsing on-line greater than three hours lately,
    but I never found any fascinating article like yours. It is beautiful price sufficient for me.
    In my view, if all webmasters and bloggers made just right content material as you did, the web shall be
    a lot more helpful than ever before.

    Reply
  34. It is appropriate time to make a few plans for the longer term and it’s time to be happy.
    I’ve learn this submit and if I may just I desire to counsel you
    few interesting issues or suggestions. Maybe you could write subsequent articles referring to this article.

    I want to read more issues approximately it!

    Reply
  35. An added important aspect is that if you are a senior, travel insurance regarding pensioners is something you should really take into consideration. The more mature you are, a lot more at risk you will be for having something undesirable happen to you while overseas. If you are never covered by a number of comprehensive insurance plan, you could have some serious difficulties. Thanks for sharing your advice on this blog.

    Reply
  36. I’ve been surfing on-line greater than 3 hours nowadays,
    yet I by no means discovered any fascinating article like
    yours. It’s lovely price sufficient for me. Personally, if all
    website owners and bloggers made excellent content as you did, the web will likely be much more
    useful than ever before.

    Reply
  37. I have been surfing online more than three hours today, yet I never found any interesting article like yours.
    It is pretty worth enough for me. In my opinion, if all web owners and bloggers
    made good content as you did, the web will be
    much more useful than ever before.

    Reply
  38. Woah! I’m really digging the template/theme of this website.
    It’s simple, yet effective. A lot of times it’s tough to get that “perfect balance” between user friendliness and visual appeal.
    I must say you have done a superb job with this.

    Also, the blog loads very quick for me on Opera.
    Superb Blog!

    Reply
  39. Hi, I do believe this is an excellent web site. I stumbledupon it 😉 I am going to
    come back once again since I saved as a favorite it.
    Money and freedom is the greatest way to change,
    may you be rich and continue to help other people.

    Reply
  40. I will immediately seize your rss feed as I can’t to find your e-mail subscription hyperlink or e-newsletter service.
    Do you’ve any? Kindly let me know so that I may subscribe.

    Thanks.

    Reply
  41. I’ve been browsing online more than 2 hours today, yet I never found any interesting article like yours.
    It’s pretty worth enough for me. In my view, if all webmasters and bloggers made good content as you did, the web will
    be a lot more useful than ever before.

    Reply
  42. Its such as you learn my mind! You appear to grasp
    so much about this, such as you wrote the e book in it
    or something. I feel that you just can do with some % to
    force the message house a bit, however other
    than that, that is wonderful blog. A great read. I’ll definitely be back.

    Reply
  43. Greetings from Ohio! I’m bored to tears at work so I decided to
    browse your website on my iphone during lunch break. I enjoy the info you present here
    and can’t wait to take a look when I get home. I’m shocked at how
    quick your blog loaded on my phone .. I’m not even using WIFI, just 3G ..
    Anyhow, fantastic site!

    Reply
  44. Pingback: buy viagra
  45. Pingback: viagra
  46. I like what you guys are up too. Such intelligent work and reporting! Keep up the superb works guys I’ve incorporated you guys to my blogroll. I think it’ll improve the value of my site :).

    Reply
  47. Pingback: cialis australia
  48. Pingback: viagra samples
  49. It’s appropriate time to make some plans for the future and it’s time to be happy.
    I’ve read this post and if I could I desire to suggest you few interesting
    things or tips. Maybe you can write next articles referring to this article.
    I desire to read more things about it!

    Reply
  50. Pingback: viagra buy online
  51. Pingback: cialis 10mg
  52. Pingback: buy sildenafil