Title: async iterators/generators in stream.pipeline() · Issue #27140 · nodejs/node · GitHub
Open Graph Title: async iterators/generators in stream.pipeline() · Issue #27140 · nodejs/node
X Title: async iterators/generators in stream.pipeline() · Issue #27140 · nodejs/node
Description: Apologies for all the code here. We can consume a Readable stream using an async iterator: // source: http://2ality.com/2018/04/async-iter-nodejs.html async function printAsyncIterable(iterable) { for await (const chunk of iterable) { co...
Open Graph Description: Apologies for all the code here. We can consume a Readable stream using an async iterator: // source: http://2ality.com/2018/04/async-iter-nodejs.html async function printAsyncIterable(iterable) { ...
X Description: Apologies for all the code here. We can consume a Readable stream using an async iterator: // source: http://2ality.com/2018/04/async-iter-nodejs.html async function printAsyncIterable(iterable) { ...
Opengraph URL: https://github.com/nodejs/node/issues/27140
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"async iterators/generators in stream.pipeline()","articleBody":"Apologies for all the code here.\r\n\r\nWe can consume a `Readable` stream using an async iterator:\r\n\r\n```js\r\n// source: http://2ality.com/2018/04/async-iter-nodejs.html\r\nasync function printAsyncIterable(iterable) {\r\n for await (const chunk of iterable) {\r\n console.log('\u003e\u003e\u003e '+chunk);\r\n }\r\n}\r\n\r\nprintAsyncIterable(fs.createReadStream('my-file.txt', 'utf8'));\r\n```\r\n\r\nAnd we can use async generators similarly to how one would use a `Transform` stream: \r\n\r\n```js\r\n/**\r\n * Parameter: async iterable of chunks (strings)\r\n * Result: async iterable of lines (incl. newlines)\r\n */\r\nasync function* chunksToLines(chunksAsync) {\r\n let previous = '';\r\n for await (const chunk of chunksAsync) {\r\n previous += chunk;\r\n let eolIndex;\r\n while ((eolIndex = previous.indexOf('\\n')) \u003e= 0) {\r\n // line includes the EOL\r\n const line = previous.slice(0, eolIndex+1);\r\n yield line;\r\n previous = previous.slice(eolIndex+1);\r\n }\r\n }\r\n if (previous.length \u003e 0) {\r\n yield previous;\r\n }\r\n}\r\n\r\n/**\r\n * Parameter: async iterable of lines\r\n * Result: async iterable of numbered lines\r\n */\r\nasync function* numberLines(linesAsync) {\r\n let counter = 1;\r\n for await (const line of linesAsync) {\r\n yield counter + ': ' + line;\r\n counter++;\r\n }\r\n}\r\n```\r\n\r\nThen, we can \"pipe\" these together like so:\r\n\r\n```js\r\nasync function main() {\r\n printAsyncIterable(\r\n numberLines(\r\n chunksToLines(\r\n fs.createReadStream('my-file.txt', 'utf8')\r\n )\r\n )\r\n );\r\n}\r\nmain();\r\n```\r\n\r\nThat's neat, but also kind of hideous. What if we could leverage `stream.pipeline()` to do something like this?\r\n\r\n```js\r\nasync function main() {\r\n stream.pipeline(\r\n fs.createReadStream('my-file.txt', 'utf8'),\r\n chunksToLines,\r\n numberLines,\r\n printAsyncIterable\r\n );\r\n}\r\nmain();\r\n```\r\n\r\nI'm unfamiliar with the guts of `stream.pipeline()`--and completely new to async iterators and generators--so don't know how feasible something like this is.\r\n\r\nFWIW, the \"hideous nested function calls\" can be naively replaced by use of the godlike `Array.prototype.reduce()`:\r\n\r\n```js\r\nconst pipeline = async (...args) =\u003e args.reduce((acc, arg) =\u003e arg(acc));\r\n\r\nasync function main() {\r\n pipeline(\r\n fs.createReadStream('my-file.txt', 'utf8'),\r\n chunksToLines,\r\n numberLines,\r\n printAsyncIterable\r\n );\r\n}\r\nmain();\r\n```\r\n\r\nReference: https://twitter.com/b0neskull/status/1115325542566227968","author":{"url":"https://github.com/boneskull","@type":"Person","name":"boneskull"},"datePublished":"2019-04-08T20:23:29.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":10},"url":"https://github.com/27140/node/issues/27140"}
| 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:fca5d91c-421e-e218-05c7-a55c1d7110bc |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | ED62:7C22D:55EFA2:7300A5:6A504A0C |
| html-safe-nonce | 2dfb5f8a8d6ec34e6a5f604f02a541390cd5b460b823f32316581503a509fd53 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFRDYyOjdDMjJEOjU1RUZBMjo3MzAwQTU6NkE1MDRBMEMiLCJ2aXNpdG9yX2lkIjoiNzM4Mjc5OTkyNDYxMjU4MTkwMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 405a640d4c9fcab4bd5aa6773e64d3754d9ef218f4487b3cb383768935590990 |
| hovercard-subject-tag | issue:430641255 |
| 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/nodejs/node/27140/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8ae64b73825d200fca7895911ccacce7ee149ccdeab242a7bc027c23684b5678/nodejs/node/issues/27140 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8ae64b73825d200fca7895911ccacce7ee149ccdeab242a7bc027c23684b5678/nodejs/node/issues/27140 |
| og:image:alt | Apologies for all the code here. We can consume a Readable stream using an async iterator: // source: http://2ality.com/2018/04/async-iter-nodejs.html async function printAsyncIterable(iterable) { ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | boneskull |
| hostname | github.com |
| expected-hostname | github.com |
| None | d6dc8294eb500fa36bbc57472d61fe87c522f9c3c1d64f70f4926f66a66a7efb |
| turbo-cache-control | no-preview |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| 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 | 5741a45fe07e5f377d0d4c524a92e3056acc51bb |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width