Skip to content

Instantly share code, notes, and snippets.

@seansullivan
Created March 6, 2014 02:26
Show Gist options
  • Save seansullivan/9381057 to your computer and use it in GitHub Desktop.
Save seansullivan/9381057 to your computer and use it in GitHub Desktop.
Simple promise chain w/ q using result from previous function
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);
});
@seansullivan
Copy link
Author

Output should be

method1
method2
from method1
method3
from method2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment