Skip to content

Instantly share code, notes, and snippets.

@calendee
Last active January 7, 2016 18:34
Show Gist options
  • Save calendee/7683499 to your computer and use it in GitHub Desktop.
Save calendee/7683499 to your computer and use it in GitHub Desktop.
A simple example of chained promises using Q promise library.
var Q = require("q");
var slowFunction1 = function( myObject ) {
console.log("\nRunning slowFunction1");
console.log(myObject);
var deferred = Q.defer();
setTimeout( function() {
console.log("slowFunction1 is resolving!");
myObject.slowFunction1Processed = true;
deferred.resolve( myObject );
}, 1500);
return deferred.promise;
};
var slowFunction2 = function( myObject ) {
console.log("\nRunning slowFunction2");
console.log(myObject);
var deferred = Q.defer();
setTimeout( function() {
console.log("slowFunction2 is resolving!")
myObject.slowFunction2Processed = true;
deferred.resolve( myObject );
}, 1500);
return deferred.promise;
};
var slowFunction3 = function( myObject ) {
console.log("\nRunning slowFunction3");
console.log(myObject);
var deferred = Q.defer();
setTimeout( function() {
console.log("slowFunction3 is resolving!")
myObject.slowFunction3Processed = true;
deferred.resolve( myObject );
}, 1500);
return deferred.promise;
};
var processes = [ slowFunction1, slowFunction2, slowFunction3 ];
var initialValue = { "stuff" : "it"};
var getFinalResult = function() {
return processes.reduce( function( nextProcess, f ) {
return nextProcess.then(f);
}, Q(initialValue));
};
//
//getFinalResult().then(
// function( response) {
// console.log("\nFinished running !");
// console.log(initialValue);
//
// console.log("\nHere is the returned response");
// console.log(response);
// }
//);
var badSlowFunction = function( myObject ) {
console.log("\nRunning badSlowFunction");
console.log(myObject);
var deferred = Q.defer();
setTimeout( function() {
console.log("badSlowFunction is rejecting!")
myObject.badSlowFunction = true;
deferred.reject( myObject );
}, 1500);
return deferred.promise;
};
var processes = [ slowFunction1, badSlowFunction, slowFunction3 ];
getFinalResult().then(
function( response) {
console.log("\nFinished running !");
console.log(initialValue);
console.log("\nHere is the returned response");
console.log(response);
},
function( error ) {
console.log("Oh crappers! Something rejected!");
console.log(error);
}
);
@calendee
Copy link
Author

FYI : Commented out the first getFinalResult()…, added a new badSlowFunction and then run the getFinalResult. You can now see what happens when one of they asynchronous calls rejects.

@jmorrisIII
Copy link

@calendee Is there a way to have the functions parameters supplied beforehand.Not have the next functions parameters supplied by the previous. I am having a hard time understanding.I asked a question on stackoverflow http://stackoverflow.com/questions/28074248/node-js-sequentially-running-multiple-childprocess-execfile-processes-from-q-all. I basically need the functions in an array with the parameters already supplied then have them run in order. Something like this http://stackoverflow.com/questions/4908378/javascript-array-of-functions/13812956#13812956

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