-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
640bfb8
commit 5023aa1
Showing
3 changed files
with
80 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
const { AbortError } = require('internal/errors'); | ||
const compose = require('internal/streams/compose'); | ||
|
||
module.exports.map = function map(stream, fn) { | ||
return compose(stream, async function* (source, { signal }) { | ||
for await (const item of source) { | ||
if (signal.aborted) { | ||
throw new AbortError('The iteration has been interrupted'); | ||
} | ||
yield await fn(item, { signal }); | ||
} | ||
}); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const { | ||
Readable, | ||
} = require('stream'); | ||
const assert = require('assert'); | ||
const { setTimeout } = require('timers/promises'); | ||
|
||
{ | ||
// Map works on synchronous streams with a synchronous mapper | ||
const stream = Readable.from([1, 2, 3, 4, 5]).map((x) => x + x); | ||
const result = [2, 4, 6, 8, 10]; | ||
(async () => { | ||
for await (const item of stream) { | ||
assert.strictEqual(item, result.shift()); | ||
} | ||
})().then(common.mustCall()); | ||
} | ||
|
||
{ | ||
// Map works on synchronous streams with an asynchronous mapper | ||
const stream = Readable.from([1, 2, 3, 4, 5]).map(async (x) => { | ||
await Promise.resolve(); | ||
return x + x; | ||
}); | ||
const result = [2, 4, 6, 8, 10]; | ||
(async () => { | ||
for await (const item of stream) { | ||
assert.strictEqual(item, result.shift()); | ||
} | ||
})().then(common.mustCall()); | ||
} | ||
|
||
{ | ||
// Map works on asynchronous streams with a asynchronous mapper | ||
const stream = Readable.from([1, 2, 3, 4, 5]).map(async (x) => { | ||
return x + x; | ||
}).map((x) => x * x); | ||
const result = [4, 8, 12, 16, 20]; | ||
(async () => { | ||
for await (const item of stream) { | ||
assert.strictEqual(item, result.shift()); | ||
} | ||
})().then(common.mustCall()); | ||
} | ||
|
||
{ | ||
// Allow cancellation of iteration through an AbortSignal | ||
|
||
const stream = Readable.from([1, 2, 3, 4, 5]).map(async (x, { signal }) => { | ||
return setTimeout(1e15, { signal }); | ||
}); | ||
(async () => { | ||
const iterator = stream[Symbol.asyncIterator](); | ||
iterator.next(); | ||
iterator.return(); | ||
})().catch(common.mustCall((err) => { | ||
assert.equals(err.name, 'AbortError'); | ||
})); | ||
} |