1.1.0 • Published 5 years ago

tw-async-reduce v1.1.0

Weekly downloads
1
License
ISC
Repository
-
Last release
5 years ago

Async Reduce

An async version of array.reduce().

Usage

asyncReduce functions similarly to array.reduce, but expects the callback to return a promise. Each iteration waits for the previous callback to complete before continuing the chain.

You do not need to return the accumulator from the callback, just the result for that index. To that end, the order of the callback arguments is shifted - current is the first argument, then index, then accumulator. These arguments are the same as those found in the default array.reduce() implementation.

Examples

ES6:

const urls = ['http://foo.bar', 'http://baz.yay'];

const results = await asyncReduce(urls, async (curr, index, acc) => (
  await fetch(curr)
));

ES5:

const urls = ['http://foo.bar', 'http://baz.yay'];
asyncReduce(urls, function(curr, index, acc) {
  return fetch(curr);
}).then(function(results) {});