Skip to content

reduce

使用异步函数对数组进行归约

239 bytes
since v12.1.0

使用方法

处理返回 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' } }