Closed jdbevan closed 2 years ago
That's indeed so, if you only want the resolved tasks as a result I would suggest using the Throttle.raw
as follows:
import * as Throttle from "promise-parallel-throttle";
//Function which should return a Promise
const names = [
{ firstName: "Irene", lastName: "Pullman" },
{ firstName: "Sean", lastName: "Parr" },
{ firstName: "Joe", lastName: "Slater" },
{ firstName: "Karen", lastName: "Turner" },
{ firstName: "Tim", lastName: "Black" }
];
const combineNames = (firstName, lastName) => {
return new Promise((resolve, reject) => {
if (firstName === "Joe") {
reject(new Error('Cannot have Joe'));
} else {
resolve(firstName + " " + lastName);
}
});
};
//Create a array of functions to be run
const tasks = names.map((u) => () => combineNames(u.firstName, u.lastName));
(async () => {
try {
const results = await Throttle.raw(tasks, {
maxInProgress: 1,
failFast: false
});
console.info(results.resolvedIndexes.map(index => results.taskResults[index]));
// ["Irene Pullman", "Sean Parr", "Karen Turner", "Tim Black"]
} catch (failed) {
console.error(failed.message);
}
})();
When
failFast
is set tofalse
when usingThrottle.all
you get a response type ofPromise<(T | Error)[]>
See