Last active
April 10, 2018 23:01
-
-
Save rauschma/0501ec8efa511f8021e49d704e6be99b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// node --harmony-async-iteration async-iteration-test.js | |
// for-await-of converts sync iterables over Promises to async iterables over fulfillment values | |
// Iterable<Promise<T>> -> AsyncIterable<T> | |
// Roughly: { value: Promise.resolve(123), done: false } becomes Promise.resolve({ value: 123, done: false }) | |
// Spec: https://tc39.github.io/proposal-async-iteration/#sec-createasyncfromsynciterator | |
function* gen() { | |
yield Promise.resolve(1); | |
yield Promise.resolve(2); | |
} | |
async function main() { | |
for await (const x of gen()) { | |
console.log(x); | |
} | |
} | |
main(); | |
// Output: | |
// 1 | |
// 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very cool. Thanks for sharing