Home > @infiniteobjects/core-library-web > Async > map
Map over promises concurrently
Signature:
static map<Element, NewElement>(iterable: AsyncIterable<MaybePromise<Element>> | Iterable<MaybePromise<Element>>, mapper: AsyncMapper<Element, NewElement>, options?: AsyncMapOptions): Promise<NewElement[]>;
Parameter | Type | Description |
---|---|---|
iterable | AsyncIterable<MaybePromise<Element>> | Iterable<MaybePromise<Element>> | |
mapper | AsyncMapper<Element, NewElement> | |
options | AsyncMapOptions | (Optional) |
Returns:
Promise<NewElement[]>
This is different from Promise.all()
in that you can control the concurrency and also decide whether or not to stop iterating when there’s an error.
import { Async } from '@infiniteobjects/core-library';
import got from 'got';
const sites = [
getWebsiteFromUsername('sindresorhus'), //=> Promise
'https://avajs.dev',
'https://github.com'
];
const mapper = async site => {
const {requestUrl} = await got.head(site);
return requestUrl;
};
const result = await Async.map(sites, mapper, { concurrency: 2 });
console.log(result);
//=> ['https://sindresorhus.com/', 'https://avajs.dev/', 'https://github.com/']