Skip to content

Async For Each

Sykander edited this page Feb 25, 2020 · 8 revisions

The asyncForEach() method executes a provided asynchronous function once for each array element.

Syntax

asyncArr.asyncForEach(callback(currentValue[, index[, array]])[, thisArg])

Parameters

  • callback Async function to execute on each element. It accepts between one and three arguments:
    • currentValue The current element being processed in the iterable object.
    • index |optional The index of currentValue in the iterable object.
    • array |optional The iterable object that asyncFilter was called upon.
  • thisArg optional Value to use as this when executing callback.

Return Value

undefined

Examples

Sending updates to multiple apis

console.log('Starting update.');

const update = 'Important message!';
await AsyncArray.from(['foo.bar', 'baz.qux']).asyncForEach(async api => {
  const { status, message } = await http.post(api, update);

  console.log(api, status, message);
});

console.log('Done updating.');
Starting Update.
foo.bar, 200, Updated
baz.qux, 200, Updated
Done updating.
Clone this wiki locally