2023-08-22 09:30:34 -04:00
|
|
|
/**
|
|
|
|
* run `fn` in serie for all values, and resolve with an array of the results
|
|
|
|
* inspired by https://stackoverflow.com/a/50506360/1314820
|
|
|
|
* @template T the input array's item type
|
|
|
|
* @template V the `fn` function's return type
|
|
|
|
* @param {T[]} values
|
|
|
|
* @param {(item: T) => Promise<V>} fn
|
|
|
|
* @returns {V[]}
|
|
|
|
*/
|
2020-11-26 09:22:30 -05:00
|
|
|
export function mapSeries(values, fn) {
|
2023-08-22 09:30:34 -04:00
|
|
|
return values.reduce(async (promiseChain, value) => {
|
|
|
|
const chainResults = await promiseChain
|
|
|
|
const currentResult = await fn(value)
|
|
|
|
return [...chainResults, currentResult]
|
2020-11-26 09:22:30 -05:00
|
|
|
}, Promise.resolve([]))
|
|
|
|
}
|