forEachBail.js 553 B

12345678910111213141516171819202122232425
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. module.exports = function forEachBail(array, iterator, callback) {
  7. if (array.length === 0) return callback();
  8. let i = 0;
  9. const next = () => {
  10. let loop = undefined;
  11. iterator(array[i++], (err, result) => {
  12. if (err || result !== undefined || i >= array.length) {
  13. return callback(err, result);
  14. }
  15. if (loop === false) while (next());
  16. loop = true;
  17. });
  18. if (!loop) loop = false;
  19. return loop;
  20. };
  21. while (next());
  22. };