Last active
December 13, 2015 18:08
-
-
Save alexcurtis/4952689 to your computer and use it in GitHub Desktop.
Refactoring with JQuery 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
function GetPopularServices(id) { | |
var d = $.Deferred(); | |
var popularServiceUrl = PopularServiceUrl(id); | |
$.getJSON(popularServiceUrl, {}, function (data) { | |
CreatePopularService(this, function(service) { | |
//... | |
//Resolve The Promise | |
d.resolve({ services: [service] }); | |
}); | |
}).error(function () { ShowErrorDialog(); }); | |
return d.promise(); | |
} | |
function GetCategoryServices(id) { | |
var d = $.Deferred(); | |
var categoryServiceUrl = CategoryServiceUrl(id); | |
$.getJSON(categoryServiceUrl, {}, function (data) { | |
var services = []; | |
$(data).each(function () { | |
CreateService(this, function(service) { | |
//... | |
services.push(service); | |
}); | |
//Resolve The Promise When Done | |
}).promise().done(function () { d.resolve({ services: services }); }); | |
}).error(function () { ShowErrorDialog(); }); | |
return d.promise(); | |
} | |
function PostProcessService(service) { | |
//... | |
} | |
//Get Services For id | |
function GetServices(id) { | |
$.when(GetPopularServices(id), GetCategoryServices(id)).done(function (pop, cat) { | |
PostProcessService(pop); | |
PostProcessService(cat); | |
ShowResults(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment