Returning values after async call

glockysoywowl

New Member
Hi i have been trying to do the following.
I have a module in a system i am currently designing which is as follows\[code\]var queryDispatcher = { init: function () { var g = new Array() ; var b = new Array() ; var y = new Array() ; var total_google = 0 ; var total_bing = 0 ; var total_yahoo = 0 ; return { callGoogle: function (query) { var url = 'http://localhost/meta/public/google/'+query ; $.getJSON(url, function(data) { total_google = data.searchInformation.totalResults; var i = 0 ; $.each(data.items, function() { var obj = new res(i+1, this.title, this.snippet, this.link, 0) ; g.push(obj) ; i=i+1 ; }); }); return true ; }, callBing: function (query) { var url = 'http://localhost/meta/public/bing/'+query ; $.getJSON(url, function(data) { total_bing = data.d.results[0].WebTotal; var j = 0 ; $.each(data.d.results[0].Web, function() { var obj = new res(j+1, this.Title, this.Description, this.Url, 0) ; b.push(obj) ; j=j+1 ; }); }); }, callYahoo: function (query) { var url = 'http://localhost/meta/public/yahoo/'+query ; $.getJSON(url, function(data) { total_yahoo = data.bossresponse.web.totalresults; var k = 0 ; $.each(data.bossresponse.web.results, function() { var obj = new res(k+1, this.title, this.abstract, this.url, 0) ; y.push(obj) ; k=k+1 ; }); }); }, getGoogle: function () { }}}};\[/code\]Now if for example i instantiate queryDispatcher as follows: \[code\]var qd = queryDispatcher.init(); var google = qd.callGoogle("hey there"); \[/code\]I should be able to retrieve the results. Currently the results are being pushed into the array g which is empty initally and because of the async getJSON call, when i try to return g it is always empty. How can i wait for the async call to be completed so i can return the array g and the total_google variable?
 
Back
Top