Skip to content

Instantly share code, notes, and snippets.

@ferreiratiago
Created June 14, 2018 08:53
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 ferreiratiago/27f3f526fade79e91f685329fda6a2f9 to your computer and use it in GitHub Desktop.
Save ferreiratiago/27f3f526fade79e91f685329fda6a2f9 to your computer and use it in GitHub Desktop.
Async Iterable
var asyncIterable = {
[Symbol.asyncIterator]: async function* asyncGenerator() {
var array = [Promise.resolve(1), Promise.resolve(2)];
while (array.length) {
// it waits for the promise to resolve
// before yield the value
yield await array.shift();
}
}
};
(async function() {
// it waits for each item to resolve
// before moving to the next()
for await (const item of asyncIterable) {
console.log(item);
// 1 2
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment