Skip to content

Commit

Permalink
Merge pull request #1830 from balena-io/no-delay-last
Browse files Browse the repository at this point in the history
Do not add a delay after the final item in `throttledForEach`
  • Loading branch information
Page- authored Nov 1, 2024
2 parents b337110 + 7b7ea8b commit 904e725
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,28 @@ export const throttledForEach = async <T, U>(
fn: (item: T) => PromiseLike<U> | U,
): Promise<U[]> => {
const promises: Array<PromiseLike<U> | U> = [];
const runFn = async (item: T): Promise<void> => {
try {
const p = fn(item);
promises.push(p);
await p;
} catch {
// Ignore, the caller can handle the error if it needs to but
// due to the delays if we don't add a catch handler here then
// it'll always be treated as an unhandled error
}
};
// Handle the last item separately so we don't add a delay after it
const lastItem = array.pop();
for (const item of array) {
// We do not wait for each individual fn, we just throttle the calling of them
promises.push(fn(item));
void runFn(item);
// Delay by the throttle rate before we continue to the next item
await setTimeout(delayMS);
}
if (lastItem != null) {
void runFn(lastItem);
}
// We return the results of the iterator so the caller can await them as necessary
return await Promise.all(promises);
};
Expand Down

0 comments on commit 904e725

Please sign in to comment.