parallel.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /** @license MIT License (c) copyright 2011-2013 original author or authors */
  2. /**
  3. * parallel.js
  4. *
  5. * Run a set of task functions in parallel. All tasks will
  6. * receive the same args
  7. *
  8. * @author Brian Cavalier
  9. * @author John Hann
  10. */
  11. (function(define) {
  12. define(function(require) {
  13. var when = require('./when');
  14. var all = when.Promise.all;
  15. var slice = Array.prototype.slice;
  16. /**
  17. * Run array of tasks in parallel
  18. * @param tasks {Array|Promise} array or promiseForArray of task functions
  19. * @param [args] {*} arguments to be passed to all tasks
  20. * @return {Promise} promise for array containing the
  21. * result of each task in the array position corresponding
  22. * to position of the task in the tasks array
  23. */
  24. return function parallel(tasks /*, args... */) {
  25. return all(slice.call(arguments, 1)).then(function(args) {
  26. return when.map(tasks, function(task) {
  27. return task.apply(void 0, args);
  28. });
  29. });
  30. };
  31. });
  32. })(typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); });