I have a JavaScript code that I need to convert into a queue system. Currently, the code looks messy with long chains of requests being made in a specific order. Please note that my function requests return JSON objects and setting async to false is not an option for cross-domain JSONP calls. Calls must be made in a particular sequence and jQuery queues are not suitable.
var customers;
var orders;
var products;
function GetCustomers(){
$.ajax({
url: somecrossdomainurl?calback=GetCustomerCallback,
dataType: 'jsonp',
async: false
});
}
function GetCustomerCallback(data){
customers = data;
GetCustomersOrder();
}
function GetCustomersOrder(){
$.ajax({
url: somecrossdomainurl?calback=GetCustomersOrderCallback,
dataType: 'jsonp',
async: false
});
}
function GetCustomersOrderCallback(data){
orders = data;
GetOrderProducts();
}
function GetOrderProducts(){
$.ajax({
url: somecrossdomainurl?calback=GetOrderProductsCallback,
dataType: 'jsonp',
async: false
});
}
function GetOrderProductsCallback(data){
products = data;
DisplayCustomersAndOrder();
}
function DisplayCustomersAndOrder(){
//loop through customer, order, products and display info
}
Any suggestions on how to improve this code?