I'm updating a "legacy" React application which fetches data asynchronously through $.ajax
.
The data fetched is a mandatory property for a child component, so it should be available right away. I thought I'd fetch the data in the constructor. It was solved until now with a call to ReactDOM.render
in the done
callback, but this has some drawbacks (i.e. it won't re-render properly on viewport changes).
The problem boils down to this:
$.when(API.fetchSomething(), API.fetchAnother()).done(([firstResult], [secondResult]) => {
console.log("run this first");
this.first = firstResult;
this.second = secondResult;
}).someMagicWaitMethod();
console.log("then run this");
console.log(this.first, this.second); // must be available right now!
How can I wait in a proper way for the async calls to finish until continuing with the execution?