You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
796 B

4 years ago
  1. 'use strict';
  2. module.exports = asyncForEach;
  3. /**
  4. * Simultaneously processes all items in the given array.
  5. *
  6. * @param {array} array - The array to iterate over
  7. * @param {function} iterator - The function to call for each item in the array
  8. * @param {function} done - The function to call when all iterators have completed
  9. */
  10. function asyncForEach (array, iterator, done) {
  11. if (array.length === 0) {
  12. // NOTE: Normally a bad idea to mix sync and async, but it's safe here because
  13. // of the way that this method is currently used by DirectoryReader.
  14. done();
  15. return;
  16. }
  17. // Simultaneously process all items in the array.
  18. let pending = array.length;
  19. array.forEach(item => {
  20. iterator(item, () => {
  21. if (--pending === 0) {
  22. done();
  23. }
  24. });
  25. });
  26. }