-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.mjs
60 lines (56 loc) · 1.28 KB
/
test.mjs
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { ReadableStream } from 'stream/web';
function delay(ms) {
return new Promise((resolve, rej) => {
setTimeout(resolve, ms);
});
}
try {
let i = 0;
const readableStream = new ReadableStream({
start(control) {
setInterval(() => {
control.enqueue(i++);
}, 100);
// setTimeout(() => {
// control.error('read error');
// }, 1000);
},
pull(control) {
// control.enqueue('11');
// undefined.length;
// control.close();
// control.error('error');
// undefined.length;
},
cancel(reason) {
console.log('---------', reason);
},
});
setTimeout(() => {
console.log('cancel');
}, 2000);
await readableStream.pipeTo(
new WritableStream({
async write(chunk, controller) {
console.log(chunk);
await delay(1);
controller.error('error');
// if (chunk === 7) {
// throw 'error';
// }
},
close() {
console.log('close------WritableStream');
},
abort(reason) {
console.log('abort--------');
},
})
);
console.log('end--------');
// for await (const iterator of readableStream) {
// console.log(iterator);
// }
} catch (error) {
console.log('---end---', error);
}