Skip to content

Instantly share code, notes, and snippets.

@yuri
Last active August 29, 2015 14:09
Show Gist options
  • Save yuri/62988af42efc4b920dd3 to your computer and use it in GitHub Desktop.
Save yuri/62988af42efc4b920dd3 to your computer and use it in GitHub Desktop.
Promise-Aware Compose
function pcompose() {
var step1;
var step2;
var tailArgs;
// TODO: Handle the case where only one argument is passed.
if (arguments.length === 2) {
step1 = arguments[1];
step2 = arguments[0];
return function(items) {
var value = step1(items);
if (value.then) {
return value
.then(function(result) {
return step2(result);
});
} else {
return step2(value);
}
}
} else {
tailArgs = Array.prototype.slice.call(arguments, 1);
return pcompose(arguments[0], pcompose.apply(null, tailArgs));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment