Javascript 为什么';t';等待&x27;等待

Javascript 为什么';t';等待&x27;等待,javascript,asynchronous,async-await,Javascript,Asynchronous,Async Await,我不熟悉Javascript中的wait/async函数。我编写了以下函数: getGoogleResults = async () => { const googleResultUrl = 'http://www.google.com/search?q=bakery&num=60'); // get only Divs within results that have class of 's' $.getJSON(googleResultUrl, (da

我不熟悉Javascript中的wait/async函数。我编写了以下函数:

getGoogleResults = async () => {
    const googleResultUrl = 'http://www.google.com/search?q=bakery&num=60');

    // get only Divs within results that have class of 's'
    $.getJSON(googleResultUrl, (data) => {
      const results = $(data.contents).find('div[class="g"]');
      const divs = results.find('div[class="s"]');
      console.log(data.contents); // data logged here looks fine in console
      return data.contents; 
    });
  }
这个函数工作得很好,我可以将响应注销到控制台并查看解析出来的数据(前60个Google结果)

然而,我不明白的是,当我调用函数时,我希望它等到承诺返回后再继续。但事实并非如此。当我调用该函数时,程序立即运行下一行(控制台日志),无需等待:

async startProcess() {
    const googleResults = await this.getGoogleResults();
    console.log(googleResults); // this gets run immediately, produces 'undefined' in console
  }
并且记录到控制台的是“未定义”。所以,很明显,我做错了什么,但我找不到一个例子来说明这可能是什么。这就像程序调用函数,但立即继续,而不等待承诺

编辑:我知道我的函数现在只是返回
数据。内容
,而不是解析的div。此时我正在测试,希望在调用异步函数后看到结果。

您需要从函数返回一个对象
getGoogleResults

getGoogleResults = () => { // You don't need the async keyword here
    return new Promise((resolve, reject) => {
        const googleResultUrl = 'http://www.google.com/search?q=bakery&num=60');
        // get only Divs within results that have class of 's'
        $.getJSON(googleResultUrl, (data) => {
          const results = $(data.contents).find('div[class="g"]');
          const divs = results.find('div[class="s"]');
          console.log(data.contents); // data logged here looks fine in console
          resolve(data.contents); // Use the function resolve.
        });

        // Use the function reject for any errors.
    }); 
}
您需要从函数
getGoogleResults

getGoogleResults = () => { // You don't need the async keyword here
    return new Promise((resolve, reject) => {
        const googleResultUrl = 'http://www.google.com/search?q=bakery&num=60');
        // get only Divs within results that have class of 's'
        $.getJSON(googleResultUrl, (data) => {
          const results = $(data.contents).find('div[class="g"]');
          const divs = results.find('div[class="s"]');
          console.log(data.contents); // data logged here looks fine in console
          resolve(data.contents); // Use the function resolve.
        });

        // Use the function reject for any errors.
    }); 
}

您应该返回一个承诺,保证您的异步函数实际上是可以等待的。像这样:

getGoogleResults = async () => {
    return new Promise((resolve, reject) => {
      const googleResultUrl = 'http://www.google.com/search?q=bakery&num=60');

      // get only Divs within results that have class of 's'
      $.getJSON(googleResultUrl, (data) => {
        const results = $(data.contents).find('div[class="g"]');
        const divs = results.find('div[class="s"]');
        console.log(data.contents); // data logged here looks fine in console
        resolve(data.contents); 
      }); //TODO call reject() on error
    }
}

您应该返回一个承诺,保证您的异步函数实际上是可以等待的。像这样:

getGoogleResults = async () => {
    return new Promise((resolve, reject) => {
      const googleResultUrl = 'http://www.google.com/search?q=bakery&num=60');

      // get only Divs within results that have class of 's'
      $.getJSON(googleResultUrl, (data) => {
        const results = $(data.contents).find('div[class="g"]');
        const divs = results.find('div[class="s"]');
        console.log(data.contents); // data logged here looks fine in console
        resolve(data.contents); 
      }); //TODO call reject() on error
    }
}

您没有从
getGoogleResults
返回任何内容,因此
等待
undefined
中返回其结果。“return data.contents”语句没有返回任何内容吗?
$.getJSON()回调中的
return
语句没有做任何有用的事情,特别是它不会影响
异步
函数的返回值。当然,但它不会影响外部函数
getGoogleResults
,该函数不会返回任何内容。如果$jquery是您不需要的';我们不需要像答案所暗示的那样回报新的承诺。只需return$.getJSON()您没有从
getGoogleResults
返回任何内容,因此
wait
未定义的
中等待其结果。`return data.contents'语句没有返回任何内容吗?
$.getJSON()回调中的
return
语句没有做任何有用的事情,特别是它不会影响
异步
函数的返回值。当然,但它不会影响外部函数
getGoogleResults
,该函数不会返回任何内容。如果$jquery是您不需要的';我们不需要像答案所暗示的那样回报新的承诺。只需返回$.getJSON()