使用异步等待参数列表后缺少) [英] missing ) after argument list using async await

查看:52
本文介绍了使用异步等待参数列表后缺少)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a file call find.js, and I run with node find.js, my node is version 10 I have no clue why I fail to use async await.

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

推荐答案

由于 async 函数

Because an async function returns an implicit Promise, you can change this:

console.log(await execute());

对此:

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

否则,您将需要在另一个 async 函数内调用 execute ,因为 await 仅可在内部使用>异步功能.

Otherwise, you would need to have your call to execute inside of another async function because await can only be used inside of async functions.

这篇关于使用异步等待参数列表后缺少)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆