Skip to content

Instantly share code, notes, and snippets.

@hallettj
Created August 1, 2012 02:08
Show Gist options
  • Save hallettj/3222798 to your computer and use it in GitHub Desktop.
Save hallettj/3222798 to your computer and use it in GitHub Desktop.
$.flatMap, generalizes running asynchronous operations in sequence
$.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();
};
@hallettj
Copy link
Author

hallettj commented Aug 1, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment