2

嗨,我一直在尝试执行以下操作。
我目前正在设计的系统中有一个模块,如下所示

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 () {
      }
}
}
};

现在,例如,如果我按如下方式实例化 queryDispatcher:

var qd = queryDispatcher.init(); 
var google = qd.callGoogle("hey there"); 

我应该能够检索结果。目前,结果被推入最初为空的数组 g 中,并且由于异步 getJSON 调用,当我尝试返回 g 时,它始终为空。

如何等待异步调用完成,以便返回数组 g 和 total_google 变量?

4

0 回答 0