我有一个名为find.js的文件,我使用节点find.js运行,我的节点是版本10我不知道为什么我不能使用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发布于 2020-04-17 12:20:13
因为是async函数returns an implicit Promise,所以您可以更改以下内容:
console.log(await execute());要这样做:
execute().then(res => console.log(res));否则,您将需要在另一个async函数中调用execute,因为await只能在async函数中使用。
发布于 2020-04-17 12:18:06
将execute函数放入async函数中
async function test() {
const result = await execute();
console.log("result = ", result);
}
test();https://stackoverflow.com/questions/61264054
复制相似问题