Created
August 1, 2012 02:08
-
-
Save hallettj/3222798 to your computer and use it in GitHub Desktop.
$.flatMap, generalizes running asynchronous operations in sequence
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
$.flatMap = function(promise, f) { | |
var deferred = $.Deferred(); | |
function reject(/* arguments */) { | |
// The reject() method puts a deferred into its failure | |
// state. | |
deferred.reject.apply(deferred, arguments); | |
} | |
promise.then(function(/* values... */) { | |
var newPromise = f.apply(null, arguments); | |
newPromise.then(function(/* newValues... */) { | |
deferred.resolve.apply(deferred, arguments); | |
}, reject); | |
}, reject); | |
return deferred.promise(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See http://sitr.us/2012/07/31/promise-pipelines-in-javascript.html for details.