Last active
August 29, 2015 14:14
-
-
Save appkr/135faee654ebfb9bb991 to your computer and use it in GitHub Desktop.
jQuery anatomy of Deferred
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($) { | |
var myVar; | |
var setVal = function() { | |
var deferred = $.Deferred(); | |
setTimeout(function() { | |
myVar = 'myValue'; | |
deferred.resolve(); | |
}, 2000); | |
return deferred.promise(); | |
}; | |
setVal().done(function() { | |
console.log('All done! ' + myVar); | |
}); | |
})(jQuery); | |
(function($) { | |
$.fetchYouTube = function(feed) { | |
return $.ajax({ | |
url: "http://gdata.youtube.com/feeds/mobile/users/" + (feed || "TEDtalksDirector") + "/uploads", | |
data: {orderby: "published", alt: "json", v: 2}, | |
dataType: "json" | |
}).promise(); | |
}; | |
$.fetchYouTube("TEDtalksDirector") | |
.done(function(results) { | |
console.log(results.feed.entry); | |
}).fail(function() { | |
console.log("Some bad thing happened!"); | |
}); | |
})(jQuery); | |
(function($) { | |
function fetchYouTubeFeed(userName) { | |
return $.ajax({ | |
url: "http://gdata.youtube.com/feeds/mobile/users/" + (userName || "TEDtalksDirector") + "/uploads", | |
data: {orderby: "published", alt: "json", v: 2}, | |
dataType: "json" | |
}).promise(); | |
}; | |
$.when(fetchYouTubeFeed('Jayesslee'), fetchYouTubeFeed('davidchoimusic')) | |
.done(function(results1, results2) { | |
console.log(results1[0].feed.entry); | |
console.log(results2[0].feed.entry); | |
}).fail(function() { | |
console.log("Some bad thing happened!"); | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment