mirror of
https://github.com/vatesfr/xen-orchestra.git
synced 2026-11-09 17:06:13 -06:00
- add missing syntaxes - don't put prompt if no command outputs to ease copy/paste and use `sh` syntax - always use `$` as prompt and use `console` syntax
90 lines
2.4 KiB
Markdown
90 lines
2.4 KiB
Markdown
<!-- DO NOT EDIT MANUALLY, THIS FILE HAS BEEN GENERATED -->
|
|
|
|
# @xen-orchestra/async-map
|
|
|
|
[](https://npmjs.org/package/@xen-orchestra/async-map)  [](https://bundlephobia.com/result?p=@xen-orchestra/async-map) [](https://npmjs.org/package/@xen-orchestra/async-map)
|
|
|
|
> Promise.all + map for all iterables
|
|
|
|
## Install
|
|
|
|
Installation of the [npm package](https://npmjs.org/package/@xen-orchestra/async-map):
|
|
|
|
```sh
|
|
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.
|
|
|
|
```js
|
|
import { asyncMap } from '@xen-orchestra/async-map'
|
|
|
|
const array = await asyncMap(iterable, iteratee, thisArg)
|
|
```
|
|
|
|
It can be used with any iterables (`Array`, `Map`, etc.):
|
|
|
|
```js
|
|
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:
|
|
|
|
```js
|
|
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.
|
|
|
|
```js
|
|
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](https://github.com/vatesfr/xen-orchestra/issues)
|
|
you've encountered;
|
|
- fork and create a pull request.
|
|
|
|
## License
|
|
|
|
[ISC](https://spdx.org/licenses/ISC) © [Vates SAS](https://vates.fr)
|