Created
April 1, 2014 00:28
-
-
Save patrickleet/9905348 to your computer and use it in GitHub Desktop.
Promises
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
// A Promise is an object that represents a task with two possible outcomes (success or failure) and holds callbacks that fire | |
// when one outcome or the other has occurred. | |
// But the biggest advantage of using Promises is that you can easily derive new Promises from existing ones. | |
// You might ask two Promises representing parallel tasks to give you a Promise that will inform you of their mutual completion. | |
// Or you might ask a Promise representing the first task in a series to give you a Promise representing the final task in | |
// the series. | |
// Without Promises | |
$.get('/mydata', { | |
success: onSuccess, | |
failure: onFailure, | |
always: onAlways | |
}); | |
// With Promises | |
var gettingData = $.get('/mydata'); | |
gettingData.done(onSuccess); | |
gettingData.fail(onFailure); | |
gettingData.always(onAlways); | |
//One task, one callback. | |
var gettingData = $.get('/mydata'); | |
gettingData.done(useDataForTaskOne); | |
gettingData.done(useDataForTaskTwo); | |
// A Promise is either fulfilled or rejected; jQuery uses resolved and rejected | |
// Deferreds | |
// A Deferred is a Promise! More precisely, it’s a superset of Promise with one critical addition: | |
// You can trigger a Deferred directly. A pure Promise only lets you add more callbacks; | |
// someone else has to trigger them. | |
// In other words, you can not resolve a promise, you can only add callbacks to it! | |
// If you need to control when a promise is resolved or rejected, use a Deferred. | |
// You can get a promise from a deferred. | |
// Example | |
var doSomething = function() { | |
var deferred = new $.Deferred(); | |
reallyDoSomething(function(err, result) { | |
if (err) deferred.reject(); | |
deferred.resolve(result); | |
}) | |
return deferred.promise(); | |
} | |
// usage | |
var doingSomething = doSomething(); | |
doingSomething.done(onSuccess); | |
// jQuery ajax requests all return promises since jQuery 1.5 | |
// To reiterate: Every Deferred has a single Promise, and every Promise represents a Deferred. | |
// Ultimately, every Promise is either resolved(fulfilled) or rejected. | |
// Mainly, because programmers thrive on binary | |
// We know exactly how to put 1s and 0s together to perform astounding feats of logic. | |
// That’s a big reason why Promises are so powerful: Promises let us see concurrent tasks as booleans. | |
// When multiple Promises complete | |
var gettingDataSetOne = $.get('/dataset1'); | |
var gettingDataSetTwo = $.get('/dataset2'); | |
$.when(gettingDataSetOne, gettingDataSetTwo).done(onBothDone); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment