Last active
August 29, 2015 14:25
-
-
Save catdad/ef106e154c27df712571 to your computer and use it in GitHub Desktop.
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
// 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); | |
} | |
); | |
} | |
} |
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
// 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