Jen*_*fer 0 javascript ecmascript-6 axios
我有一个文件调用 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
Run Code Online (Sandbox Code Playgroud)
因为一个async函数返回一个隐式的 Promise,你可以改变这个:
console.log(await execute());
Run Code Online (Sandbox Code Playgroud)
对此:
execute().then(res => console.log(res));
Run Code Online (Sandbox Code Playgroud)
否则,您需要execute在另一个async函数内部调用,因为await只能在async函数内部使用。