xen-orchestra/@xen-orchestra/async-map
2023-11-07 12:32:38 +01:00
..
.npmignore feat: unified .npmignore for all packages 2021-04-07 13:58:14 +02:00
.USAGE.md chore: hide USAGE.md 2022-02-18 17:11:52 +01:00
index.js chore: enforce strict mode for CJS files 2022-02-22 12:34:41 +01:00
index.test.js test(async-map): from Jest to test (#6484) 2022-10-25 16:17:08 +02:00
legacy.js chore: enforce strict mode for CJS files 2022-02-22 12:34:41 +01:00
package.json chore: update dev deps 2023-11-07 12:32:38 +01:00
README.md docs: uniformize code blocks 2023-02-06 11:25:12 +01:00

@xen-orchestra/async-map

Package Version License PackagePhobia Node compatibility

Promise.all + map for all iterables

Install

Installation of the npm package:

npm install --save @xen-orchestra/async-map

Usage

asyncMap(iterable, iteratee, thisArg = iterable)

Similar to Promise.all + Array#map for all iterables: calls iteratee for each item in iterable, and returns a promise of an array containing the awaited result of each calls to iteratee.

It rejects as soon as te first call to iteratee rejects.

import { asyncMap } from '@xen-orchestra/async-map'

const array = await asyncMap(iterable, iteratee, thisArg)

It can be used with any iterables (Array, Map, etc.):

const map = new Map()
map.set('foo', 42)
map.set('bar', 3.14)

const array = await asyncMap(map, async function ([key, value]) {
  // TODO: do async computation
  //
  // the map can be accessed via `this`
})

Use with plain objects

Plain objects are not iterable, but you can use Object.keys, Object.values or Object.entries to help:

const object = {
  foo: 42,
  bar: 3.14,
}

const array = await asyncMap(
  Object.entries(object),
  async function ([key, value]) {
    // TODO: do async computation
    //
    // the object can be accessed via `this` because it's been passed as third arg
  },
  object
)

asyncMapSettled(iterable, iteratee, thisArg = iterable)

Similar to asyncMap but waits for all promises to settle before rejecting.

import { asyncMapSettled } from '@xen-orchestra/async-map'

const array = await asyncMapSettled(iterable, iteratee, thisArg)

Contributions

Contributions are very welcomed, either on the documentation or on the code.

You may:

  • report any issue you've encountered;
  • fork and create a pull request.

License

ISC © Vates SAS