我正在嘗試使用 Promise.all() 函數對所有請求進行等待,而不是像這樣手動執行所有獲?。簐ar data = await Promise.all([ fetch('https://jsonplaceholder.typicode.com/posts').then((response) => response.json()), fetch('https://jsonplaceholder.typicode.com/albums').then((response) => response.json()), fetch('https://jsonplaceholder.typicode.com/users').then((response) => response.json()) ]);我想讓它動態化,像這樣發出 N 個獲取請求: let promiseList = []; try { for (let url of requestUrls) { promiseList.push(fetch(url).then((response) => response.json())); } var data = await Promise.all(promiseList);但我得到這個錯誤Uncaught SyntaxError: await is only valid in async function的await Promise.all()路線,如果我刪除的await,我得到一個Promise {<pending>}和 (index):79 error:TypeError: data is not iterable這是我的完整代碼:https : //jsfiddle.net/ham7g82e/1/從這些提取中獲取數據我缺少什么?
JS'await 僅在 Promise.all() 函數中的異步函數中有效
DIEA
2021-06-09 18:05:45