Skip to content

Instantly share code, notes, and snippets.

@asimmittal
Last active July 23, 2022 18:00
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 asimmittal/27076f07c337ea41af15a022725d88c9 to your computer and use it in GitHub Desktop.
Save asimmittal/27076f07c337ea41af15a022725d88c9 to your computer and use it in GitHub Desktop.
/*
* 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