loading
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
type CurriedPromise<T> = () => Promise<T>;
type ChunkedArray<T> = Array<Array<T>>;

function getData(msg: string): CurriedPromise<string> {
  return () => Promise.resolve(msg);
}

const fifo: Array<CurriedPromise<string>> = [
  getData('1'), getData('2'), getData('3'), getData('4'),
];

const PER_CHUNK = 2;
const chunks = fifo.reduce<ChunkedArray<CurriedPromise<string>>>(
  (acc, curr, i) => {
    const chunkIndex = Math.floor(i / PER_CHUNK);

    if (!acc[chunkIndex]) {
      acc[chunkIndex] = [curr];
    } else {
      acc[chunkIndex].push(curr);
    }

    return acc;
  },
  [],
);

(async function doWork() {
  for (const chunk of chunks) {
    const res = await Promise.all(chunk.map((f) => f()));
    console.log(res);
  }
})();
Fetching token