Created
February 20, 2015 02:37
-
-
Save DavidAnson/80c516c2067cc1375d83 to your computer and use it in GitHub Desktop.
Qsequence, a simple sequencer for Q
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
"use strict"; | |
var Q = require("q"); | |
// Worker function | |
function logAndIncrement(val) { | |
console.log(val); | |
return val + 1; | |
} | |
// Normal | |
Q(0) | |
.then(function(v) { | |
return Q(logAndIncrement(v)); | |
}) | |
.then(function(v) { | |
return Q(logAndIncrement(v)); | |
}) | |
.then(function(v) { | |
console.log(v); | |
}); | |
// Slightly modified from https://github.com/kriskowal/q#sequences | |
function Qsequence(initialValue, promises) { | |
var result = Q(initialValue); | |
promises.forEach(function(r) { | |
result = result.then(r); | |
}); | |
return result; | |
} | |
// With Qsequence | |
Qsequence(0, [ | |
function(v) { | |
return Q(logAndIncrement(v)); | |
}, | |
function(v) { | |
return Q(logAndIncrement(v)); | |
}, | |
function(v) { | |
console.log(v); | |
} | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment