@wesjet/function.js
Version:
wesjet javascript library
35 lines (34 loc) • 1.28 kB
JavaScript
/**
* Copyright (c) Wesbitty, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*
*/
/** Promise.all + Array.map */
export const promiseMap = (arr, map) => Promise.all(arr.map(map));
export const promiseMapDict = async (dict, map) => {
const mappedEntries = await Promise.all(Object.entries(dict).map(async ([key, val]) => [key, await map(val)]));
return Object.fromEntries(mappedEntries);
};
export const promiseMapToDict = async (arr, mapValue, mapKey) => {
const mappedEntries = await Promise.all(arr.map(async (el, index) => [mapKey(el, index), await mapValue(el, index)]));
return Object.fromEntries(mappedEntries);
};
export const promiseMapPool = async (arr, map, poolLimit) => {
const ret = [];
const executing = [];
for (const [index, item] of arr.entries()) {
const p = Promise.resolve().then(() => map(item, index));
ret.push(p);
if (poolLimit <= arr.length) {
const e = p.then(() => executing.splice(executing.indexOf(e), 1));
executing.push(e);
if (executing.length >= poolLimit) {
await Promise.race(executing);
}
}
}
return Promise.all(ret);
};