Created
June 14, 2018 08:53
-
-
Save ferreiratiago/27f3f526fade79e91f685329fda6a2f9 to your computer and use it in GitHub Desktop.
Async Iterable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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