I have built a single page application where users can build reports. The users are presented with a form allowing them to choose the data source, the chart type and the theme, then upon confirmation the page loads the required files and draws the chart.
For better performance, I’d like to load the code modules and data in parallel. For example if the user selects “Pie chart”, “Blue theme” and “Airline Stats”:
WHEN (js module Pie chart is loaded)
and (blue theme css is loaded)
and (Airline Stats json is loaded)
THEN (draw chart)
I have found a number of libraries that implement AMD, and a number of libraries that implement promises, but none where module loading and promises could be combined as in my example above. Is this possible, and are there any libraries that already implement this?
My need is for client side JavaScript.
jQuery can actualy do this through promises. It’s just a matter of modifying the code.
Assuming that you own the code and all live in the same domain or if cross-domain, the server allows CORS, we will load each with
.ajax(). We will then use.when()to detect when all the promises are loaded and.then()to add our callback to execute all the promises resolve.