Skip to content

Instantly share code, notes, and snippets.

@cromwellryan
Created October 11, 2013 12:31
Show Gist options
  • Save cromwellryan/6933859 to your computer and use it in GitHub Desktop.
Save cromwellryan/6933859 to your computer and use it in GitHub Desktop.
composable promises from args (ie requirejs dependencies)
var Q = require('q');
console.log();
function hi() {
Q.promise( function() {
console.log("hi: " + Date());
});
}
function bye() {
console.log("bye: " + Date());
}
function what() {
var deferred = Q.defer();
function done() {
console.log("what?: " + Date());
deferred.resolve();
}
setTimeout( done, 2000 );
return deferred.promise;
}
function doall() {
args(arguments).reduce( function(seq, next) {
return seq.then(next);
}, Q()).done();
}
function args(a) {
var args = [];
for(var key in a) {
args.push( a[key] );
}
console.log(args);
return args;
}
doall(hi,bye,what);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment