做法
使用 async/await
的方式,有順序性地等待 Fetch
回應。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| async function fetchData(url, defer) { const response = await fetch(url);
await (() => new Promise((resolve) => setTimeout(() => resolve(), defer)))();
return await response.json(); }
(async () => { console.log(await fetchData('url_1', 1000));
console.log(await fetchData('url_2', 500));
console.log(await fetchData('url_3', 0)); })();
|
參考資料