Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
* ajaxGET
* -- Custom wrapper around the Jquery GET method
* -- when the request is complete, it returns the response using
* the 'finish' callback
*/
var ajaxGET = function(url, finish){
$.get(url).done(function(data){
finish(data);
});
}
/*
* Main - start here
*/
$(document).ready(function(){
//1 - get google
var first = function(){
return new Promise(function(resolve){
ajaxGET("http://google.com",function(googData){
console.log("---> got first");
//do something with googData
resolve();
});
});
}
//2 - get apple
var second = function(){
return new Promise(function(resolve){
ajaxGET("http://apple.com",function(appleData){
console.log("---> got second");
//do something with appleData
resolve();
});
});
}
//3 - get amazon
var third = function(){
return new Promise(function(resolve){
ajaxGET("http://amazon.com",function(amData){
console.log("---> got third");
//do something with amData
resolve();
});
});
}
//Start sequential GET using chained promises
first().then(second).then(third);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment