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