Title: Asynchronous Iterations · Issue #11326 · microsoft/TypeScript · GitHub
Open Graph Title: Asynchronous Iterations · Issue #11326 · microsoft/TypeScript
X Title: Asynchronous Iterations · Issue #11326 · microsoft/TypeScript
Description: The asynchronous iterations proposal is now in stage 3, so it should be safe to support that in TypeScript now. Currently, a function may be a generator or an async function, but it cannot be both. The asynchronous iteration proposal rem...
Open Graph Description: The asynchronous iterations proposal is now in stage 3, so it should be safe to support that in TypeScript now. Currently, a function may be a generator or an async function, but it cannot be both....
X Description: The asynchronous iterations proposal is now in stage 3, so it should be safe to support that in TypeScript now. Currently, a function may be a generator or an async function, but it cannot be both....
Opengraph URL: https://github.com/microsoft/TypeScript/issues/11326
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Asynchronous Iterations","articleBody":"The [asynchronous iterations](https://github.com/tc39/proposal-async-iteration) proposal is now in stage 3, so it should be safe to support that in TypeScript now. Currently, a function may be a generator or an async function, but it cannot be both. The asynchronous iteration proposal removes this restriction. Here's an example from their readme:\n\n``` javascript\nasync function* readLines(path) {\n let file = await fileOpen(path);\n\n try {\n while (!file.EOF) {\n yield await file.readLine();\n }\n } finally {\n await file.close();\n }\n}\n```\n## Types\n\nGenerator functions declare several interfaces:\n\n``` typescript\ninterface IteratorResult\u003cT> {\n done: boolean;\n value: T;\n}\ninterface Iterator\u003cT> {\n next(value?: any): IteratorResult\u003cT>;\n return?(value?: any): IteratorResult\u003cT>;\n throw?(e?: any): IteratorResult\u003cT>;\n}\ninterface Iterable\u003cT> {\n [Symbol.iterator](): Iterator\u003cT>;\n}\ninterface IterableIterator\u003cT> extends Iterator\u003cT> {\n [Symbol.iterator](): IterableIterator\u003cT>;\n}\n```\n\nAsync generators return promises instead of their `IteratorResult`, and use a new symbol, `Symbol.asyncIterator`:\n\n``` typescript\ninterface IteratorResult\u003cT> {\n done: boolean;\n value: T;\n}\ninterface AsyncIterator\u003cT> {\n next(value?: any): Promise\u003cIteratorResult\u003cT>>;\n return?(value?: any): Promise\u003cIteratorResult\u003cT>>;\n throw?(e?: any): Promise\u003cIteratorResult\u003cT>>;\n}\ninterface AsyncIterable\u003cT> {\n [Symbol.asyncIterator](): AsyncIterator\u003cT>;\n}\ninterface AsyncIterableIterator\u003cT> extends AsyncIterator\u003cT> {\n [Symbol.asyncIterator](): AsyncIterableIterator\u003cT>;\n}\n```\n## Emit\n\nI guess that this would require another helper function to downlevel to ES2015, similar to the one used for async functions. Yield and await expression would both be replaced by yield expression, so they need to be decorated with some tag, like `yield ['await', promise]` for `await promise`.\n## for-await-of\n\nThe proposal also introduces a new loop. An example from their readme:\n\n``` javascript\nfor await (const line of readLines(filePath)) {\n console.log(line);\n}\n```\n\nThis iterates over the `[Symbol.asyncIterator]()` object and awaits the value of `.next()`, similar to a for-of loop. This loop can only be used in async functions.\n","author":{"url":"https://github.com/ivogabe","@type":"Person","name":"ivogabe"},"datePublished":"2016-10-03T17:13:06Z","url":"https://github.com/microsoft/TypeScript/issues/11326"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:c952022a-859a-9341-d2d2-77dfbaf1fa51 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DAC0:311640:228FC0:2CD3D5:6B00CC7A |
| html-safe-nonce | a928dffb5f2ab805f83bde56cc854ea1119ecbf998c67f73122d06df43648342 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQUMwOjMxMTY0MDoyMjhGQzA6MkNEM0Q1OjZCMDBDQzdBIiwidmlzaXRvcl9pZCI6IjM4OTk0NDc1MDA0MjMwODkyNzQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | cd2213eeff9faa3593eed59ef65e1f447eabdba83e54220923ae165abfa341fe |
| hovercard-subject-tag | issue:180694785 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/11326/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5acf85ae52e38f1fce4eacf1cd87d0fc24576144845866086734f548db02ee85/microsoft/TypeScript/issues/11326 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5acf85ae52e38f1fce4eacf1cd87d0fc24576144845866086734f548db02ee85/microsoft/TypeScript/issues/11326 |
| og:image:alt | The asynchronous iterations proposal is now in stage 3, so it should be safe to support that in TypeScript now. Currently, a function may be a generator or an async function, but it cannot be both.... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ivogabe |
| hostname | github.com |
| expected-hostname | github.com |
| None | 98dc00e171c204462a6f367482a0f8c3c6b5dbca99cd7390e82d2df49ce096e4 |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ef013399cf0a3c324bea644a685d7c1e0f06b5b7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width