Last active
August 29, 2015 14:09
-
-
Save yuri/62988af42efc4b920dd3 to your computer and use it in GitHub Desktop.
Promise-Aware Compose
This file contains 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
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