chore(utils): optimized promiseAllSequence

This commit is contained in:
Marek Polak
2023-07-13 13:33:57 +02:00
committed by Marek Polák
parent 8294ffaf0e
commit 971fd1d8b4

View File

@@ -12,14 +12,13 @@ export const promiseAllSequence = async <
actions: Fn[],
) => {
const results: R[] = [];
await actions.reduce(
(promise, fn) =>
// 2. then call action, store result and repeat
promise.then(fn).then(result => {
results.push(result);
return result;
}),
Promise.resolve(), // 1. Resolve initial promise
);
// For some reason, the previous implementation with promise chaining
// (https://github.com/trezor/trezor-suite/blob/100015c45451ed50e2b0906d78de73c0fd2883d1/packages/utils/src/promiseAllSequence.ts)
// was significantly slower in some cases, therefore simple for cycle is used instead
for (let i = 0; i < actions.length; ++i) {
// eslint-disable-next-line no-await-in-loop
const result = await actions[i]();
results.push(result);
}
return results;
};