Skip to content

Instantly share code, notes, and snippets.

@rauschma
Last active April 10, 2018 23:01
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rauschma/0501ec8efa511f8021e49d704e6be99b to your computer and use it in GitHub Desktop.
Save rauschma/0501ec8efa511f8021e49d704e6be99b to your computer and use it in GitHub Desktop.
// 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
@austinfrey
Copy link

Very cool. Thanks for sharing

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