-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex6.js
More file actions
33 lines (27 loc) · 988 Bytes
/
ex6.js
File metadata and controls
33 lines (27 loc) · 988 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
'use strict';
/*
Problem:
Implement `pump(iterable, sink, { signal })`.
Behavior:
- Read chunks from async iterable.
- Write each chunk to sink via `sink.write(chunk)`.
- If write returns false, await `sink.drain()` before continuing.
- Respect AbortSignal: when aborted, stop writing and call `sink.close()`.
- Do not write extra chunks after abort.
Starter code is intentionally incorrect:
- Ignores backpressure (`write` return value).
- Ignores abort signal.
- Never closes sink on abort.
*/
async function pump(iterable, sink, { signal } = {}) {
if (!iterable || typeof iterable[Symbol.asyncIterator] !== 'function') {
throw new TypeError('iterable must be async iterable');
}
if (!sink || typeof sink.write !== 'function' || typeof sink.drain !== 'function' || typeof sink.close !== 'function') {
throw new TypeError('sink must expose write, drain, and close');
}
for await (const chunk of iterable) {
sink.write(chunk);
}
}
module.exports = { pump };