I want to get two resources using two asynch calls. I want to proceed only when both resources have been retrieved.
How can I do this elegantly in JS?
This would work:
getStuff1(function (result1) {
getStuff2 (function (result2) {
// do stuff with result1 and result2
....
}
}
but stuff2 only starts after stuff1 completes. I’d prefer to start stuff2 while waiting on stuff1.
If you know that functions are in fact first-class objects in Javascript, you can come up with a fairly elegant solution.
Without any extra objects, or global variables.
Why is this so elegant? Because you’ve encapsulated the data, your scope is free from useless variables and the code is more readable than ever. How cool is that? 🙂
And if you want a bit more general solution you may try the following:
Awesomeness again 🙂