Javascript函数未返回变量

时间:2018-08-18 23:29:10

标签: javascript

我的问题是与此answer.

相关的链接

我在下面尝试过同样的事情,但是它仍然说它是未定义的。

function Get(callback) {
    var xhr = new XMLHttpRequest();
    
    xhr.responseType = 'json';
    xhr.onload = function() {
      var status = xhr.status;
      if (status === 200) {
        callback(null, xhr.response);
      } else {
        callback(status, xhr.response);
      }
    };
    xhr.open('GET', 'https://search.mtvnservices.com/typeahead/suggest/?solrformat=true&rows=20&q=alexander+mccormick%20AND%20schoolid_s%3A1262&defType=edismax&qf=teacherfirstname_t%5E2000%20teacherlastname_t%5E2000%20teacherfullname_t%5E2000%20autosuggest&bf=pow(total_number_of_ratings_i%2C2.1)&sort=total_number_of_ratings_i%20desc&siteName=rmp&rows=20&start=0&fl=pk_id%20teacherfirstname_t%20teacherlastname_t%20total_number_of_ratings_i%20averageratingscore_rf%20schoolid_s&fq=', true);
    xhr.send();
};

var result;
Get(function (err, result) {
    result = result.response.docs.map(doc => doc.averageratingscore_rf);
});
console.log(result);
}

我知道,当我将代码调整为此时,它确实可以抓住我想要的原因

Get(function (err, result) {
    console.log(result.response.docs.map(doc => doc.averageratingscore_rf));
});

输出不是不确定的,它说的是2.3,这就是我想要的可变结果。

2 个答案:

答案 0 :(得分:0)

围绕诺言

function Get(callback) {
    return new Promise(function(res){
        var xhr = new XMLHttpRequest();

        xhr.responseType = 'json';
        xhr.onload = function() {
          var status = xhr.status;
          if (status === 200) {
            res(callback(null, xhr.response));
          } else {
            res(callback(status, xhr.response));
          }
        };
        xhr.open('GET', 'https://search.mtvnservices.com/typeahead/suggest/?solrformat=true&rows=20&q=alexander+mccormick%20AND%20schoolid_s%3A1262&defType=edismax&qf=teacherfirstname_t%5E2000%20teacherlastname_t%5E2000%20teacherfullname_t%5E2000%20autosuggest&bf=pow(total_number_of_ratings_i%2C2.1)&sort=total_number_of_ratings_i%20desc&siteName=rmp&rows=20&start=0&fl=pk_id%20teacherfirstname_t%20teacherlastname_t%20total_number_of_ratings_i%20averageratingscore_rf%20schoolid_s&fq=', true);
        xhr.send();
    })
};



 Get(function (err, result) {
        return result.response.docs.map(doc => doc.averageratingscore_rf);
    })
   .then(function(result){
        //do whatever you want here
        console.log(result)
    });

如果您在ES5中工作,请在ajax周围创建对象包装器,或编写一个递归函数以等待该变量可用。

答案 1 :(得分:0)

在javascript中,您通常会异步执行操作:

function Get(callback) {
    var xhr = new XMLHttpRequest();

    xhr.responseType = 'json';
    xhr.onload = function() {
        var status = xhr.status;
        if (status === 200) {
            callback(null, xhr.response);
        } else {
            callback(status, xhr.response);
        }
    };
    xhr.open('GET', 'https://search.mtvnservices.com/typeahead/suggest/?solrformat=true&rows=20&q=alexander+mccormick%20AND%20schoolid_s%3A1262&defType=edismax&qf=teacherfirstname_t%5E2000%20teacherlastname_t%5E2000%20teacherfullname_t%5E2000%20autosuggest&bf=pow(total_number_of_ratings_i%2C2.1)&sort=total_number_of_ratings_i%20desc&siteName=rmp&rows=20&start=0&fl=pk_id%20teacherfirstname_t%20teacherlastname_t%20total_number_of_ratings_i%20averageratingscore_rf%20schoolid_s&fq=', true);
    xhr.send();
};

var res;
Get(function (err, result) {
    res = result.response.docs.map(doc => doc.averageratingscore_rf);
    console.log('res has been set because the xhr call has returned, res: ', res);
});
console.log('res is not YET available, res: ', res);

执行顺序将在控制台中自行说明。