![](/img/trans.png)
[英]Uncaught SyntaxError: await is only valid in async function (FireBase documentation)
[英]Uncaught SyntaxError: await is only valid in async function in async function
在異步函數中調用getRevenueByID函數,但獲取未捕獲的SyntaxError。 我在這做錯了什么?
(async() =>{
try {
response = await fetch(mainURL);
data = await response.json();
console.log(data);
console.log(data.results[0].title);
ID_Array = [].concat.apply([], data.results.map(d => d.id))
console.log(ID_Array);
getRevenueByID(ID_Array);
} catch (error) {
console.log(error);
}
})();
getRevenueByID = (arr => {
for (let i = 0; i < arr.length; i++){
console.log("ID is: ", arr[i]);
getRevenueURL = await fetch('someurl' + arr[i] + '?api_key=YOUR_KEY&language=en-US');
console.log(getRevenueURL);
// let data = await getRevenueURL.json();
// console.log(data);
}
});
getRevenueByID
本身不是異步函數。
“await僅在異步函數中有效”表示“直接進入”而不是“從callstack中的某個地方調用”。
所以讓它異步:
getRevenueByID = async (arr) => {
// ...
};
然后等待它回到你所說的地方:
await getRevenueByID(ID_Array);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.