Skip to content

2821. Delay the Resolution of Each Promise

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
type Fn = () => Promise<any>;

function delayAll(functions: Fn[], ms: number): Fn[] {
  return functions.map((fn) => {
    return async () => {
      await new Promise((resolve) => setTimeout(resolve, ms));
      try {
        const result = await fn();
        return Promise.resolve(result);
      } catch (error) {
        return Promise.reject(error);
      }
    };
  });
}