@neverendingqs/

promise-batches-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.09996703037472199","path":"index.js","file":{"path":"index.js","content":{"asEncoding":{"base64":"J3VzZSBzdHJpY3QnOwoKY29uc3QgcmVxdWVzdFRvRXh0ZXJuYWxTZXJ2aWNlID0gZnVuY3Rpb24oZCkgewogIC8vIFJlcGxhY2Ugd2l0aCBhIHByb21pc2UgdGhhdCBkb2VzIHJlYWwgd29yayBoZXJlLi4uCiAgcmV0dXJuIG5ldyBQcm9taXNlKHJlc29sdmUgPT4gewogICAgY29uc29sZS5sb2coZCk7CgogICAgLy8gRGVsYXkgZGVtb25zdHJhdGVzIHdlIGFyZSBpbmRlZWQgYmF0Y2hpbmcKICAgIHNldFRpbWVvdXQocmVzb2x2ZSwgMjUwKTsKICB9KTsKfTsKCmNvbnN0IGNodW5rID0gKGFycmF5LCBiYXRjaFNpemUgPSA1KSA9PiB7CiAgY29uc3QgY2h1bmtlZCA9IFtdOwogIGZvcihsZXQgaSA9IDA7IGkgPCBhcnJheS5sZW5ndGg7IGkgKz0gYmF0Y2hTaXplKSB7CiAgICBjaHVua2VkLnB1c2goYXJyYXkuc2xpY2UoaSwgaSArIGJhdGNoU2l6ZSkpCiAgfQoKICByZXR1cm4gY2h1bmtlZDsKfQoKLy8gUmVwbGFjZSB3aXRoIHJlYWwgZGF0YQpjb25zdCBjaHVua2VkRGF0YSA9IGNodW5rKFsuLi5BcnJheSgyMSkua2V5cygpXSk7CmNvbnNvbGUubG9nKGNodW5rZWREYXRhKTsKLyoKICogWyBbIDAsIDEsIDIsIDMsIDQgXSwKICogICBbIDUsIDYsIDcsIDgsIDkgXSwKICogICBbIDEwLCAxMSwgMTIsIDEzLCAxNCBdLAogKiAgIFsgMTUsIDE2LCAxNywgMTgsIDE5IF0sCiAqICAgWyAyMCBdIF0KKi8KCmNvbnN0IHJlZHVjZXIgPSAoY2hhaW4sIGJhdGNoKSA9PiBjaGFpbgogIC50aGVuKCgpID0+IFByb21pc2UuYWxsKAogICAgYmF0Y2gubWFwKGQgPT4gcmVxdWVzdFRvRXh0ZXJuYWxTZXJ2aWNlKGQpKQogICkpOwoKY29uc3QgcHJvbWlzZUNoYWluID0gY2h1bmtlZERhdGEucmVkdWNlKAogIHJlZHVjZXIsCiAgUHJvbWlzZS5yZXNvbHZlKCkKKTsKcHJvbWlzZUNoYWluLnRoZW4oKCkgPT4gY29uc29sZS5sb2coJ2RvbmUnKSk7"},"asBuffer":null},"loaded":true}}
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
34
35
36
37
38
39
40
41
42
'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);
  });
};

const chunk = (array, batchSize = 5) => {
  const chunked = [];
  for(let i = 0; i < array.length; i += batchSize) {
    chunked.push(array.slice(i, i + batchSize))
  }

  return chunked;
}

// Replace with real data
const chunkedData = chunk([...Array(21).keys()]);
console.log(chunkedData);
/*
 * [ [ 0, 1, 2, 3, 4 ],
 *   [ 5, 6, 7, 8, 9 ],
 *   [ 10, 11, 12, 13, 14 ],
 *   [ 15, 16, 17, 18, 19 ],
 *   [ 20 ] ]
*/

const reducer = (chain, batch) => chain
  .then(() => Promise.all(
    batch.map(d => requestToExternalService(d))
  ));

const promiseChain = chunkedData.reduce(
  reducer,
  Promise.resolve()
);
promiseChain.then(() => console.log('done'));
node v10.16.0