25
loading...
This website collects cookies to deliver better user experience
// jQuery based pseudo-code
$.ajax({
...
success: function(){
// handle success
},
error: function(){
// handle error
}
});
// jQuery AJAX call
$.ajax({
url: "https://api.github.com/users/saurabh-misra/repos",
success: function(repos) {
// jQuery AJAX call
$.ajax({
url: repos[2].url,
success: function(repoInfo) {
console.log("Name: ", repoInfo.name);
console.log("Description: ", repoInfo.description);
},
error: function(error) {
console.error(error);
}
});
},
error: function() {
console.error(error);
}
});
/*
Name: pomodoro-timer
Description: A simple pomodoro timer web app
that helps you focus on your work.
*/
// jQuery based pseudo code
$.ajax({
success: function(response){
$.ajax({
success: function(){
$.ajax({
success: function(){
$.ajax({
success: function(){
$.ajax({
success: function(){
// handle success
}
});
}
});
}
});
}
});
}
});
// jQuery pseudo code
function doAJAXCallOne(){
$.ajax({
success: function(){
// handle success
}
});
}
function doAJAXCallTwo(){
$.ajax({
success: function(){
doAJAXCallOne();
}
});
}
function doAJAXCallThree(){
$.ajax({
success: function(){
doAJAXCallTwo();
}
});
}
function doAJAXCallFour(){
$.ajax({
success: function(){
doAJAXCallThree();
}
});
}
function doAJAXCallFive(){
$.ajax({
success: function(){
doAJAXCallFour();
}
});
}
// pseudo code
// Make ajax request to store CC info in client's payment gateway account
ajax({
success: function() {
// Make an ajax call to verify this response
ajax({
success: function() {
// Make ajax request to process one part of the payment
ajax({
success: function() {
// Make an ajax call to verify this response
ajax({
success: function() {
// Make ajax request to process second part of the payment
ajax({
success: function() {
// Make an ajax call to verify this response
ajax({
success: function() {
// Make ajax call to mark order as complete in our own API
ajax({
success: function() {
// handle final success
},
error: function() {
// handle errors
}
});
},
error: function() {
// handle errors
}
});
},
error: function() {
// handle errors
}
});
},
error: function() {
// handle errors
}
});
},
error: function() {
// handle errors
}
});
},
error: function() {
// handle errors
}
});
},
error: function() {
// handle errors
}
});
// pseudo code
function handleErrors(){ ... };
function verifyResponse( fnMakeNextAJAXCall ){
ajax({
success: function(){
fnMakeNextAJAXCall();
},
error: handleErrors
});
}
function storeCCDetails(){
ajax({
success: function(){
verifyResponse( processFirstPayment );
},
error: handleErrors
});
}
function processFirstPayment(){
ajax({
success: function(){
verifyResponse( processSecondPayment );
},
error: handleErrors
});
}
function processSecondPayment(){
ajax({
success: function(){
verifyResponse( markOrderAsComplete );
},
error: handleErrors
});
}
function markOrderAsComplete(){
ajax({
success: function(){
// handle success
},
error: handleErrors
});
}
storeCCDetails();
storeCCDetails()
at the bottom. Do you find yourself bouncing around from one function to the other? Now imagine doing that when these functions have several hundreds of lines of code inside them. // pseudo code
fetch( /*store cc details*/ )
.then( () => fetch( /*verify response*/ ))
.then( () => fetch( /*make first payment*/ ))
.then( () => fetch( /*verify response*/ ))
.then( () => fetch( /*make second payment*/ ))
.then( () => fetch( /*verify response*/ ))
.then( () => fetch( /*mark order as complete*/ ))
.catch( () => {
// handle errors
})
.finally( () => {
// perform clean up
});
success()
callback to jQuery's ajax function. Now jQuery is a pretty stable library but for example, you are using a different third-party library and you send a callback and this library has a bug and it ends up either not calling your success()
callback function or maybe calling it more than once. then()
handler and attach it to this promise. Now the execution of this code depends upon the whether the promise gets fulfilled or rejected, and the promise object resides within our own app so no more surrendering control to another service. async
-await
. That is the topic of discussion in the final article in this series. See you there!