Skip to content

Instantly share code, notes, and snippets.

@vetras
Created December 13, 2016 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vetras/4fd43acffa468bd44371a9bd2f3f0adc to your computer and use it in GitHub Desktop.
Save vetras/4fd43acffa468bd44371a9bd2f3f0adc to your computer and use it in GitHub Desktop.
// see this gist as well: https://gist.github.com/sergio-fry/3917217
// create the "promises" objects (deferred objects)
var address = $.ajax({});
var tweets = $.ajax({});
var facebook = $.ajax({});
// wait for all to be done
$.when(address, tweets, facebook).then(function (addressResult, tweetsResult, facebookResult) {
/* all solved and the result is on each function argument */
});
// wait for one to be done
$.when(address).then(function (addressResult) {
/* address promise is solved and the result is on the function argument */
});
// when / then / done / fail ?? Here:
//
// https://stackoverflow.com/questions/5436327/jquery-deferreds-and-promises-then-vs-done
//
// The callbacks attached to done() will be fired when the deferred is resolved.
// The callbacks attached to fail() will be fired when the deferred is rejected.
//
// Prior to jQuery 1.8, then() was just syntactic sugar:
promise.then(doneCallback, failCallback);
// was equivalent to
promise.done(doneCallback).fail(failCallback);
// As of 1.8, then() is an alias for pipe() and returns a new promise
// see this for pipe: https://stackoverflow.com/questions/9583783/when-should-i-use-jquery-deferreds-then-method-and-when-should-i-use-the-pip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment