【问题标题】:Async Function returns Promise pending异步函数返回 Promise 挂起
【发布时间】:2021-01-27 21:35:14
【问题描述】:

我不明白为什么我得到 Promise { <pending> },因为我使用了 async/await

这是我的代码

const  fetch = require("node-fetch")

function getRandomPokemon() {
    var pokemonID = Math.floor(Math.random() * 851);
    console.log("The Pokemon Id is " + pokemonID);
    return pokemonID
}

async function getJSON() {
    let response = await fetch('https://pokeapi.co/api/v2/pokemon/'+ pokemonID);
    var json = await response.json
}

var pokemonID = getRandomPokemon()
var json = getJSON()
console.log(json)

【问题讨论】:

    标签: javascript node.js async-await es6-promise


    【解决方案1】:

    三个问题:

    1. getJSON() 不返回任何内容。它需要以return json; 结尾。
    2. 声明一个函数async 使它返回一个promise。如果你想直接获取返回值而不是一个promise,你必须用await getJSON() 调用它。只有从另一个异步函数调用它时,才能这样做。
    3. 您需要拨打response.json:var json = response.json();

    【讨论】:

      【解决方案2】:

      所有async 函数都返回一个promise - 总是。在 async 函数内使用 await 会暂停该函数的执行,直到您等待的 promise 解决或拒绝,但 async 函数不会阻塞外部世界。 await 不会怀疑整个 js 解释器的执行。

      async 函数中的第一个await 处,您的函数将一个承诺返回给调用者,并且调用者继续执行。当函数最终在未来某个时间完成其工作时,该承诺将被解决或拒绝。

      我不明白为什么我会得到 Promise { },因为我使用了 async/await

      因为所有async 函数都返回一个承诺。


      您的代码中有很多问题:

      1. 您的getJSON() 函数没有返回值。这意味着它返回的承诺解析为undefined,因此无法将其值传达回。
      2. await response.json 必须是 await response.json()
      3. pokemonID 应该作为参数传递,而不仅仅是填充到更高范围的变量中。
      4. 调用getJSON()时,必须在其上使用.then()await才能从返回的promise中获取resolved值。

      您可能还注意到您的getJSON() 函数没有返回值。这意味着它返回的承诺也解析为undefined。而且,await response.json 必须是 await response.json()

      您的代码需要更像这样:

      async function getJSON(id) {
          const response = await fetch('https://pokeapi.co/api/v2/pokemon/'+ id);
          const json = await response.json();
          return json;
      }
      
      const pokemonID = getRandomPokemon();
      getJSON(pokemonID).then(data => {
          console.log(data);
      }).catch(err => {
          console.log(err);
      });
      

      【讨论】:

      • @BRUH - 请务必查看我的最新编辑,了解我更改的内容和原因的摘要。
      【解决方案3】:

      只是一些变化

      1. 将return语句添加到getJSON()函数中
      2. 使用 .json() 函数从 getJSON() 中的响应中获取 json
      3. 处理承诺的一种方法是使用 .then()。您也可以使用 await,但要确保它在 await 块内。
      const fetch = require('node-fetch');
      
      function getRandomPokemon() {
        var pokemonID = Math.floor(Math.random() * 851);
        console.log('The Pokemon Id is ' + pokemonID);
        return pokemonID;
      }
      
      async function getJSON() {
        let response = await fetch('https://pokeapi.co/api/v2/pokemon/' + pokemonID);
        var json = await response.json();
        return json
      }
      
      var pokemonID = getRandomPokemon();
      getJSON().then((json) => {
        console.log(json);
      });
      

      【讨论】:

        【解决方案4】:

        response.json 应该是一个函数。

        在您的getJSON 函数定义中,您正在执行await response.json。 将其更改为await response.json()

        注意 json() 后面的括号

        由于getJSON函数返回promise,你必须等待响应

        在倒数第二行添加var json = await getJSON();

        此外,为了使用await,您必须将代码包装在异步函数中

        将你的最后一行和倒数第二行包装在一个异步函数中

        async function resolveJSON(){
            var json = await getJSON(); // waits for the promise to fulfill
            console.log(json);
        }
        

        【讨论】:

        • 仍然承诺待处理
        • 是的,您必须等待该函数才能解析。在倒数第二行,await getJSON 函数。 var json = await getJSON()
        • @BRUH 我已经编辑了我的答案。检查它here
        猜你喜欢
        • 2020-07-09
        • 2017-10-16
        • 1970-01-01
        • 2018-04-13
        • 2019-10-23
        • 1970-01-01
        • 2019-06-04
        • 2020-06-08
        • 2023-03-08
        相关资源
        最近更新 更多