Spaces:
Sleeping
Sleeping
File size: 555 Bytes
90cbf22 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/**
* asyncMap returns the results of applying an async function over an list.
*
* @param list - Iterable object of items, e.g. an Array, Set, Object.keys
* @param asyncTransform
* @returns
*/
export async function asyncMap<FromType, ToType>(
list: Iterable<FromType>,
asyncTransform: (item: FromType, index: number) => Promise<ToType>,
): Promise<ToType[]> {
const promises: Promise<ToType>[] = [];
let idx = 0;
for (const item of list) {
promises.push(asyncTransform(item, idx));
idx += 1;
}
return Promise.all(promises);
}
|