-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stream: load stream module when using stream/promises
- Loading branch information
1 parent
47ca37d
commit 88a656a
Showing
2 changed files
with
13 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,27 @@ | ||
/* eslint-disable node-core/require-common-first, require-yield */ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { pipeline } = require('node:stream/promises'); | ||
|
||
{ | ||
// Ensure that async iterators can act as readable and writable streams | ||
async function* myCustomReadable() { | ||
yield 'Hello'; | ||
yield 'World'; | ||
} | ||
|
||
// eslint-disable-next-line require-yield | ||
const messages = []; | ||
async function* myCustomWritable(stream) { | ||
const messages = []; | ||
for await (const chunk of stream) { | ||
messages.push(chunk); | ||
} | ||
assert.deepStrictEqual(messages, ['Hello', 'World']); | ||
} | ||
|
||
pipeline( | ||
myCustomReadable, | ||
myCustomWritable, | ||
) | ||
.then(common.mustCall()); | ||
(async () => { | ||
await pipeline( | ||
myCustomReadable, | ||
myCustomWritable, | ||
); | ||
// Importing here to avoid initializing streams | ||
require('assert').deepStrictEqual(messages, ['Hello', 'World']); | ||
})() | ||
.then(require('../common').mustCall()); | ||
} |