-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved pipeline/sequence to lib/promise
refs #9178 - continue with killing our global utils folder - i haven't found any better naming for lib/promise - so, require single files for now - instead of doing `promiseLib = require('../lib/promise')` - we can optimise the requires later
- Loading branch information
0 parents
commit 137016a
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* # Pipeline Utility | ||
* | ||
* Based on pipeline.js from when.js: | ||
* https://github.com/cujojs/when/blob/3.7.4/pipeline.js | ||
*/ | ||
var Promise = require('bluebird'); | ||
|
||
function pipeline(tasks /* initial arguments */) { | ||
var args = Array.prototype.slice.call(arguments, 1), | ||
|
||
runTask = function (task, args) { | ||
// Self-optimizing function to run first task with multiple | ||
// args using apply, but subsequent tasks via direct invocation | ||
runTask = function (task, arg) { | ||
return task(arg); | ||
}; | ||
|
||
return task.apply(null, args); | ||
}; | ||
|
||
// Resolve any promises for the arguments passed in first | ||
return Promise.all(args).then(function (args) { | ||
// Iterate through the tasks passing args from one into the next | ||
return Promise.reduce(tasks, function (arg, task) { | ||
return runTask(task, arg); | ||
}, args); | ||
}); | ||
} | ||
|
||
module.exports = pipeline; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
var Promise = require('bluebird'); | ||
|
||
/** | ||
* expects an array of functions returning a promise | ||
*/ | ||
function sequence(tasks /* Any Arguments */) { | ||
var args = Array.prototype.slice.call(arguments, 1); | ||
|
||
return Promise.reduce(tasks, function (results, task) { | ||
return task.apply(this, args).then(function (result) { | ||
results.push(result); | ||
return results; | ||
}); | ||
}, []); | ||
} | ||
|
||
module.exports = sequence; |