Created
March 6, 2014 02:26
-
-
Save seansullivan/9381057 to your computer and use it in GitHub Desktop.
Simple promise chain w/ q using result from previous function
This file contains hidden or 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
var q = require('q'); | |
var method1 = function (result) { | |
var deferred = q.defer(); | |
// an async method | |
setTimeout(function () { | |
console.log('method1'); | |
deferred.resolve('from method1'); | |
}, 0); | |
return deferred.promise; | |
}, | |
method2 = function (result) { | |
var deferred = q.defer(); | |
// an async method | |
setTimeout(function () { | |
console.log('method2'); | |
console.log(result); | |
deferred.resolve('from method2'); | |
}, 0); | |
return deferred.promise; | |
}, | |
method3 = function (result) { | |
var deferred = q.defer(); | |
// an async method | |
setTimeout(function () { | |
console.log('method3'); | |
console.log(result); | |
deferred.resolve('from method3'); | |
}, 0); | |
return deferred.promise; | |
}; | |
q.fcall(method1) | |
.then(method2) | |
.then(method3) | |
.catch(function (err) { | |
console.log(err.stack); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output should be