【问题标题】:missing ) after argument list using async await在使用异步等待的参数列表之后丢失)
【发布时间】:2020-04-17 12:20:12
【问题描述】:

我有一个文件调用 find.js,我使用节点 find.js 运行,我的节点是版本 10 我不知道为什么我无法使用异步等待。

const axios = require("axios");

const execute = async () => {
  let searchResult;
  try {
    searchResult = await axios.post(
      "https://example.io/car",
      {
        ids: [12,31],
      },
      {
        headers: {
          "Accept-Language": "en-US",
        },
      }
    );
  } catch (error) {
    console.log("error", error);
  }

  return searchResult;
};

console.log(await execute()); // error on this line

【问题讨论】:

  • await execute() 放入async 函数中
  • await 关键字只能在async 函数中使用...

标签: javascript ecmascript-6 axios


【解决方案1】:

因为async函数returns an implicit Promise,你可以改变这个:

console.log(await execute());

到这里:

execute().then(res => console.log(res));

否则,您需要在另一个 async 函数内调用 execute,因为 await 只能在 async 函数内使用。

【讨论】:

    【解决方案2】:

    execute 函数放入async 函数中

    async function test() {
     const result = await execute();
     console.log("result = ", result);
    }
    
    test();
    
    

    【讨论】:

      猜你喜欢
      • 2015-09-01
      • 2017-09-20
      • 2017-07-30
      • 1970-01-01
      • 2018-02-28
      • 2019-11-13
      • 2018-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多