bulk-fs-task.js 695 B

1234567891011121314151617181920212223242526272829
  1. // Perform a set of tasks in array quickly. Sometimes testing much better for
  2. // large sets of work instead of individual promises.
  3. const bulkFsTask = (array, each) =>
  4. new Promise((resolve, reject) => {
  5. let ops = 0;
  6. const out = [];
  7. array.forEach((item, i) => {
  8. out[i] = each(item, (back, callback) => {
  9. ops++;
  10. return (err, value) => {
  11. try {
  12. out[i] = back(err, value, out[i]);
  13. } catch (e) {
  14. return reject(e);
  15. }
  16. ops--;
  17. if (ops === 0) {
  18. resolve(out);
  19. }
  20. };
  21. });
  22. });
  23. if (ops === 0) {
  24. resolve(out);
  25. }
  26. });
  27. module.exports = bulkFsTask;