@neverendingqs/

promise-serial-reduce

Nodejs

No description

fork
loading
Files
  • index.js

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.10561758164378232","path":"index.js","file":{"path":"index.js","content":{"asEncoding":{"base64":"J3VzZSBzdHJpY3QnCgpjb25zdCByZXF1ZXN0VG9FeHRlcm5hbFNlcnZpY2UgPSBmdW5jdGlvbihkKSB7CiAgLy8gUmVwbGFjZSB3aXRoIGEgcHJvbWlzZSB0aGF0IGRvZXMgcmVhbCB3b3JrIGhlcmUuLi4KICByZXR1cm4gbmV3IFByb21pc2UocmVzb2x2ZSA9PiB7CiAgICBjb25zb2xlLmxvZyhkKTsKCiAgICAvLyBEZWxheSBkZW1vbnN0cmF0ZXMgd2UgYXJlIGluZGVlZCBiYXRjaGluZwogICAgc2V0VGltZW91dChyZXNvbHZlLCAyNTApOwogIH0pOwp9OwoKLy8gUmVwbGFjZSB3aXRoIHJlYWwgZGF0YQpjb25zdCBkYXRhID0gWy4uLkFycmF5KDUpLmtleXMoKV07CmNvbnN0IHByb21pc2VDaGFpbiA9IGRhdGEucmVkdWNlKAogIChjaGFpbiwgZCkgPT4gY2hhaW4udGhlbigoKSA9PiByZXF1ZXN0VG9FeHRlcm5hbFNlcnZpY2UoZCkpLAogIFByb21pc2UucmVzb2x2ZSgpCik7Cgpwcm9taXNlQ2hhaW4udGhlbigoKSA9PiBjb25zb2xlLmxvZygnZG9uZScpKTs="},"asBuffer":null},"loaded":true}}
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.16.0