huridocs/uwazi

View on GitHub
app/shared/asyncUtils.ts

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
const sequentialPromises = async <T>(
  items: T[],
  asyncCallback: (element: T, index: number, originalArray: T[]) => Promise<void>
): Promise<void> => {
  const wrappedCallback = async (
    previousPromise: Promise<void>,
    element: T,
    index: number,
    originalArray: T[]
  ) => {
    await previousPromise;
    await asyncCallback(element, index, originalArray);
  };
  await items.reduce(wrappedCallback, Promise.resolve());
};

export { sequentialPromises };