js for await

Creator
Creator
Seonglae ChoSeonglae Cho
Created
Created
2020 Aug 21 5:56
Editor
Edited
Edited
2025 Nov 3 11:19
Refs
Refs
 
async function test() { // Regular Iterator for (const i of list) ... // Async Iterator for await (const i of Promise list) ... }
 
// example from: https://jakearchibald.com/2017/async-iterators-and-generators/ async function* asyncRandomNumbers() { const url = "https://www.random.org/integers/?num=1&min=1&max=100&col=1&base=10&format=plain&rnd=new"; while (true) { const response = await fetch(url); const text = await response.text(); yield Number(text); } } (async function() { let i = 0; for await (const number of asyncRandomNumbers()) { console.log(++i, "=>", number); if (i === 10) break; } })(); // 1 "=>" 65 // 2 "=>" 62 // ...
 
 
 
NAVER D2
 
 

Recommendations