For a web page that requires several kinds of data (i.e. for different widgets), how do you structure the API calling from the client-side? For instance, I have an application that is currently doing something like the following...
api.doOne(function (resultOne) {
api.doTwo(..., function (resultTwo) {
api.doThree(..., function (resultThree) {
api.doFour(function (resultFour) {
...
api.doTen(function(resultTen) {
finallyDisplayPage(resultTen);
... and it seems so wrong. Apart from the fact that there is major "callback-hell", it seems to defeat the purpose of asynchronous programming, because each api call is waiting for the old one to finish processing. So it is really slow as well... but what is the alternative? Should the application do the following instead?
// essential information before display
api.doOne(function (resultOne) {
api.doTwo(..., function (resultTwo) {
displayPage(resultTwo);
}
}
// the remaining can data can be inserted after the inital page load
api.doThree(..., function (resultThree) {
displayPage(resultThree);
}
api.doFour(function (resultFour) {
displayPage(resultFour);
}
...
api.doTen(function(resultTen) {
displayPage(resultTen);
}
Any advice, hints, or pointers to examples I can view online would be appreciated.