How to return the response from multiple asynchronous function calls?
-
I have a function where I do multiple asynchronous function calls. handleData function returns a Json object. I need to use these different Json objects in the draw method. Any idea how to pass result values to the draw method? I would appreciate any help. Here is my code: var publicationData = new Array(); var researchers = []; var year = []; var title = []; var pub = []; var dataJson = []; callServerAsync(); function callServerAsync(){ $.get('Year2014.html').then(function(responseData) { var result1 = handleData(responseData, dataJson); }); $.get('tauchi_publications.html').then(function(responseData) { var result2 = handleData(responseData, dataJson); }); //TO-DO //draw(result1, result2); } function handleData(responseData, dataJson){ var htmlObject = document.createElement('div'); htmlObject.innerHTML = responseData; pub = htmlObject.getElementsByClassName("julkaisu"); getPublicationData(pub); getResearchersYearTitle(publicationData); dataJson = createJson(researchers,year,title); return dataJson; } function draw(result1,result2){ result1.concat(result2); }
-
Answer:
Use promises! jQuery's $.get returns a promise for its return value. jQuery contains a $.when method for waiting for multiple deferreds. function callServerAsync(){ var p1 = $.get('Year2014.html').then(function(responseData) { return handleData(responseData, dataJson); }); var p2 = $.get('tauchi_publications.html').then(function(responseData) { return handleData(responseData, dataJson); }); // the `return` here is just for good measure return $.when(p1, p2).then(function(result1, result2){ // all your data available, can use it here. // It's in the function arguments draw(result1, result2); }); }
supaplex at Stack Overflow Visit the source
Other answers
I think you can do something like this. Where when the call finishes, it checks if the other call/results exists, if not, then it does nothing. This way the last function will actually draw it. function callServerAsync(){ var result1, result2; $.get('Year2014.html').then(function(responseData) { result1 = handleData(responseData, dataJson); if(result1 && result2) { draw(result1, result2); } }); $.get('tauchi_publications.html').then(function(responseData) { result2 = handleData(responseData, dataJson); if(result1 && result2) { draw(result1, result2); } }); }
TMP
Related Q & A:
- How to get server response with netty client?Best solution by Stack Overflow
- How to parse SOAP response with PHP?Best solution by Stack Overflow
- How to get a response from servlet via ajax?Best solution by Stack Overflow
- How to return result of function?Best solution by Stack Overflow
- How to return multiple objects of a specific library in R?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.