Skip to content

Instantly share code, notes, and snippets.

@catdad
Last active August 29, 2015 14:25
Show Gist options
  • Save catdad/ef106e154c27df712571 to your computer and use it in GitHub Desktop.
Save catdad/ef106e154c27df712571 to your computer and use it in GitHub Desktop.
// standardize a Promise with Continuation Passing Style
function PromiseCPS (promise) {
return function cpsFunc(done) {
promise.then(
function promiseSuccess() {
done.apply(undefined, [undefined].concat([].slice.call(arguments)));
}, function promiseFailure(reason) {
done(reason);
}
);
}
}
// usage
var cpsPromise1 = PromiseCPS( methodThatReturnsAPrimise() );
var cpsPromise2 = PromiseCPS( methodThatReturnsAPrimise() );
var cpsPromise3 = PromiseCPS( methodThatReturnsAPrimise() );
// example using async
async.series([
cpsPromise1,
cpsPromise2,
cpsPromise3
], function(err, results){
// do stuff
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment