Files
  • index.js
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
'use strict'

const requestToExternalService = function(d) {
  // Replace with a promise that does real work here...
  return new Promise(resolve => {
    console.log(d);

    // Delay demonstrates we are indeed batching
    setTimeout(resolve, 250);
  });
};

// Replace with real data
const data = [...Array(5).keys()];
const promiseChain = data.reduce(
  (chain, d) => chain.then(() => requestToExternalService(d)),
  Promise.resolve()
);

promiseChain.then(() => console.log('done'));
node v10.15.2 linux/amd64