Skip to content

reduce

Reduce an array with an async function

230 bytes

Usage

A reduce that handles callback functions that return a promise.

import * as _ from 'radashi'
const userIds = [1, 2, 3, 4]
const api = {
users: {
async find(id: number) {
return { name: `person ${id}` }
},
},
}
const users = await _.reduce(
userIds,
async (acc, userId) => {
const user = await api.users.find(userId)
return {
...acc,
[userId]: user,
}
},
{},
) // { 1: { name: 'person 1' }, 2: { name: 'person 2' }, 3: { name: 'person 3' }, 4: { name: 'person 4' } }