I have two (slow loading) data sources. I can start working on data source 2 as soon as it’s loaded, but need the data from both 1 and 2 to work on data from source 1. So I have some code like this:
$.get 'url2', (data2) ->
$.get 'url1', (data1) ->
# do stuff with data1 and data2
# do stuff with data2
But, I want to load them in parallel, instead of sequentially (i.e. I don’t want to wait for data source 2 to load before starting to load data source 1).
Are there any nice design patterns to do this?
use the new
$.whenfrom jQuery 1.5.here your loading them both in parallel. And using the “promises” returned from
$.getto run some function once they are ready.