Skip to content

Instantly share code, notes, and snippets.

@chandru89new
Created August 4, 2023 03:38
Show Gist options
  • Save chandru89new/1f8d7d299023a04b1384ee0b50610fe3 to your computer and use it in GitHub Desktop.
Save chandru89new/1f8d7d299023a04b1384ee0b50610fe3 to your computer and use it in GitHub Desktop.
concurrent promises with limit
const groupsOf =
(number = 0) =>
(arr = []) => {
return arr.reduce(
(acc, curr, idx) => {
const step_ = acc.step.concat(curr);
if (idx === arr.length - 1 || step_.length === number) {
return { final: acc.final.concat([step_]), step: [] };
}
return { final: acc.final, step: step_ };
},
{ final: [], step: [] },
).final;
};
const runPromise = async (promise) => {
try {
const res = await promise();
return { data: res };
} catch (e) {
return { error: e };
}
};
const runPromisesPar = async (promiseFns = []) => {
return await Promise.allSettled(promiseFns.map((p) => p()));
};
const runPromisesSeq = async (promiseFns = []) => {
let res = [];
for (let promise of promiseFns) {
res.push(await promise());
}
return res;
};
const runPromiseConcurrent =
(limit = 0) =>
async (promiseFns = []) => {
const promiseGroups = groupsOf(limit)(promiseFns).map(
(group) => async () => await runPromisesPar(group),
);
return (await runPromisesSeq(promiseGroups)).reduce(
(acc, curr) => acc.concat(curr),
[],
);
};
module.exports = {
groupsOf,
runPromisesPar,
runPromisesSeq,
runPromiseConcurrent,
};
const {
groupsOf,
runPromisesPar,
runPromisesSeq,
runPromiseConcurrent,
} = require("./index.js");
const { range } = require("lodash");
const createPromise = (val, err, timeout = 100, idx) => {
return () =>
new Promise((res, rej) => {
console.log(
`running promise #${idx} with val: ${val}, err: ${
err ? err.toString() : null
}, timeout: ${timeout}`,
);
setTimeout(() => {
if (val) {
res(val);
} else if (err) {
rej(new Error(err));
} else rej("No value or error given");
}, timeout);
});
};
const promises = range(1, 1001).map((val) => {
return createPromise(
val % 5 === 0 ? null : val,
val % 5 === 0 ? "oops" : null,
500,
val,
);
});
const main = async () => {
// const array = range(1, 11);
// console.log(groupsOf(3)(array));
// console.log(await runPromisesPar(promises));
// console.log(await runPromisesSeq(promises));
console.log(await runPromiseConcurrent(25)(promises));
};
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment