| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5ad6c20 commit 9675863
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,6 +13,8 @@ const { | |||
| 13 | 13 | const { pipelineImpl: pl } = require('internal/streams/pipeline'); | |
| 14 | 14 | const { finished } = require('internal/streams/end-of-stream'); | |
| 15 | 15 | ||
| 16 | + require('stream'); | ||
| 17 | + | ||
| 16 | 18 | function pipeline(...streams) { | |
| 17 | 19 | return new Promise((resolve, reject) => { | |
| 18 | 20 | let signal; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,27 @@ | |||
| 1 | + /* eslint-disable node-core/require-common-first, require-yield */ | ||
| 2 | + 'use strict'; | ||
| 3 | + const { pipeline } = require('node:stream/promises'); | ||
| 4 | + { | ||
| 5 | + // Ensure that async iterators can act as readable and writable streams | ||
| 6 | + async function* myCustomReadable() { | ||
| 7 | + yield 'Hello'; | ||
| 8 | + yield 'World'; | ||
| 9 | + } | ||
| 10 | + | ||
| 11 | + const messages = []; | ||
| 12 | + async function* myCustomWritable(stream) { | ||
| 13 | + for await (const chunk of stream) { | ||
| 14 | + messages.push(chunk); | ||
| 15 | + } | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + (async () => { | ||
| 19 | + await pipeline( | ||
| 20 | + myCustomReadable, | ||
| 21 | + myCustomWritable, | ||
| 22 | + ); | ||
| 23 | + // Importing here to avoid initializing streams | ||
| 24 | + require('assert').deepStrictEqual(messages, ['Hello', 'World']); | ||
| 25 | + })() | ||
| 26 | + .then(require('../common').mustCall()); | ||
| 27 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments