Closed kodie closed 6 years ago
Hey, thanks for trying this lib 😄
The all
method is pretty limited in customizations, so instead of the all you might want to use the raw
method instead which will return a result object instead of throwing the error.
Try the following code:
import * as Throttle from 'promise-parallel-throttle';
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());
} 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 { taskResults: formattedNames } = await Throttle.raw(tasks, { maxInProgress: 1, failFast: true });
} catch (failed) {
console.info(failed);
}
})();
That works great!
I modified your code a little to suit my needs:
const runTasksUntilError = async (...args) => {
args[1] = Object.assign(args[1] || {}, { failFast: true })
try {
const { taskResults } = await Throttle.raw(...args)
return taskResults
} catch (raw) {
const err = raw.taskResults.splice(raw.rejectedIndexes[0], 1)[0]
console.error(err)
return raw.taskResults
}
}
runTasksUntilError(tasks, { maxInProgress: 10 })
.then(r => {
console.log(r)
})
Thank you!
I'd like to have the Promises stop after the first error, however I'd like to return whatever results that were returned before that. Is this possible?
I've tried this with no success:
Any ideas? Thanks in advance!