【问题标题】:Using async-await with node-fetch does not return the response to the calling method将 async-await 与 node-fetch 一起使用不会将响应返回给调用方法
【发布时间】:2020-07-03 05:52:26
【问题描述】:

我在模块中定义了一个函数,该函数应该执行获取并返回响应。我无法从 fetch 返回响应。调用函数获取的返回值为“未定义”。

我是 JavaScript 和 Node 的新手,所以如果你不介意的话,可能需要一点帮助。

调用函数

async function executeTest() {
    try {
        const response = await bpc.postLendingApplication(
            blendConnection,
            loanData
        );
        console.log("Response from POST Loan: ", response);
    } catch (error) {
        console.log(error);
    }
}

执行获取请求的模块函数

const fetch = require("node-fetch");
async function postLendingApplication(connection, data) {
    console.log("Processing POST Loan.");
    await fetch(connection.url, {
        method: "POST",
        headers: connection.headers,
        body: data,
    }).then(async res => {
        console.log("Status: ", res.status);
        console.log("StatusText: ", res.statusText);
        console.log("OK: ", res.ok);
        return await res;
    });
}

控制台输出为:

Processing POST Loan.
Status:  200
StatusText:  OK
OK:  true
Response from POST Loan:  undefined

如您所见,提取完成了它应该做的事情,如果我在模块方法中记录 res.json(),它会打印有效负载。但我想从 fetch 中返回错误和响应,以便模块表现为通用方法,并且在调用方法中完成处理和错误处理。

【问题讨论】:

  • then 中不需要async/await
  • 在您的情况下,您应该返回Promise 或以其他方式重写您的函数。
  • @DaniyalLukmanov 不。 async function 自动返回一个承诺。你的返回值就是它的分辨率。
  • @Chance 你没明白我的意思。

标签: javascript node.js async-await fetch node-fetch


【解决方案1】:

你忘记了返回表单函数。 return await fetch(connection.url, { then 函数中不需要 async-await。你可以回res.json()

const fetch = require("node-fetch");
async function postLendingApplication(connection, data) {
    console.log("Processing POST Loan.");
    return await fetch(connection.url, {
        method: "POST",
        headers: connection.headers,
        body: data,
    }).then(res => {
        console.log("Status: ", res.status);
        console.log("StatusText: ", res.statusText);
        console.log("OK: ", res.ok);
        return res.json();
    });
}

【讨论】:

    【解决方案2】:

    当您将function 标记为async 时,JavaScript 将始终返回Promise,从而使其异步。当您返回一个值时,它正在解析Promise。在这些函数内部使用await 会“暂停”执行(从技术上讲,它是为await 之后发生的代码创建一个新函数),直到等待的Promise 得到解决,代替then(callback) 的使用。因此,您不需要在任何 async function 中使用 then

    但是,您确实需要将自己的async function 视为Promise

    const fetch = require("node-fetch");
    async function postLendingApplication(connection, data) {
        try {
          console.log("Processing POST Loan.");
    
          // the await eliminates the need for .then
          const res = await fetch(connection.url, {
              method: "POST",
              headers: connection.headers,
              body: data,
          })
          // this code is resumed once the fetch Promise is resolved.
          // res now has a value.
          console.log("Status: ", res.status);
          console.log("StatusText: ", res.statusText);
          return res;
       }
       catch(err) { 
         // because the promise could error, it is advised to use
         // try/catch. With a Promise, you would .then(cb).catch(errHandler)
         // but async/await doesn't utilize callbacks.
    
         // perform error handling or you can bubble it up.
        throw err
    }
    

    在调用postLendingApplication(connection, data) 时,请确保您使用的是await,如果在async functionpostLendingApplication(connection, data).then(callback) 中,则返回值将是Promise

    postLendingApplication(connection, data).then(callback).catch(errHandler)
    

    【讨论】:

    • 解决方案有效,谢谢!另一个后续问题认为,假设我从连接中删除了一个重要元素,这会导致运行时错误。如果我将您共享的所有代码都包含在 try-catch 中,那么现在的问题是调用函数失败,因为它没有返回响应对象而是异常。我可以在这里遵循最佳实践吗? catch (error) { console.log("Catch error");返回错误; }
    • 您不想返回错误。除非你做好准备,否则你想扔它。例如,您可以返回类似 `{ status: 'failure', error: err }
    • @VarunVerma 哦,没问题。 async/await 经常被误解。 async function 有点神奇。
    猜你喜欢
    • 2018-03-04
    • 2019-05-23
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    相关资源
    最近更新 更多