Skip to content

Instantly share code, notes, and snippets.

@itacirgabral
Created October 15, 2020 01:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itacirgabral/a35e8f6b2ab6f5fe8724ab3de12e8fef to your computer and use it in GitHub Desktop.
Save itacirgabral/a35e8f6b2ab6f5fe8724ab3de12e8fef to your computer and use it in GitHub Desktop.
const delay = t => new Promise(res => setTimeout(() => res(), t))
const arr = Array(5).fill()
const delayed = async function* delayed(arr, t = 1000) {
for(const el of arr) {
yield delay(t)
}
}
const it = delayed(arr)
;(async () => {
let dArr
let pDArr
let tPDArr
let notDone = true
while (notDone) {
dArr = Array(3).fill().map(el => it.next())
pDArr = Promise.all(dArr)
tPDArr = await pDArr
notDone = !tPDArr.find(el => el.done)
}
})()
@itacirgabral
Copy link
Author

for await (const el of delayed(arr)) {
  // one el at a time
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment