Map over async/promises concurrently
Control concurrent execution of async/promises functions.
npm install @dinhoabreu/p-map
const pMap = require('@dinhoabreu/p-map')
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
const main = async () => {
const list = Promise.resolve([
Promise.resolve('google.com'),
'todomvc.com',
'github.com'
])
const mapper = async site => {
await delay(50)
return `https://${site}`
}
const map = pMap(mapper, { concurrency: 2 })
const result = await map(list)
console.log(result)
// => ['https://google.com/', 'https://todomvc.com/', 'https://github.com/']
}
main()
Creates a function map(input)
.
Type: Function
Expected to return a Promise
or value.
Type: Object
Type: number
Default: Infinity
Minimum: 1
Type: boolean
Default: true
With rejectOnError
, returns a Promise
that is fulfilled when all promises in input
and ones returned from mapper
are fulfilled, or rejects if any of the promises reject. The fulfilled value is an Array
of the fulfilled values returned from mapper
in input
order.
Without rejectOnError
, returns a Promise
that is fulfilled when all promises in input
and ones returned from mapper
are fulfilled. The fulfilled value is an Array
of Array
the rejected or resolved values returned from mapper
in input
order. Check our <test/index.js> to see more.
Type: Iterable<Promise|any>
Iterated over concurrently in the mapper
function.
MIT © Edison E. Abreu