Skip to content

Instantly share code, notes, and snippets.

@rlucha
Last active May 9, 2016 14:05
Show Gist options
  • Select an option

  • Save rlucha/b7ce23677beeef5fdfd88ea444fd5d65 to your computer and use it in GitHub Desktop.

Select an option

Save rlucha/b7ce23677beeef5fdfd88ea444fd5d65 to your computer and use it in GitHub Desktop.
export function iterateAsync(iterable, fn, batch = 10) {
const iterator = iterable[Symbol.iterator]();
function iterate() {
const current = take(iterator, batch)
fn(current.result)
current.done
? () => {}
: window.requestAnimationFrame(iterate)
}
iterate()
}
function take(iterator, n) {
let result = []
let i = 0
let current = undefined;
while(i < n) {
current = iterator.next()
if(current.done) break
else(result.push(current.value), i++)
}
return {
result,
done: current.done
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment