-
Notifications
You must be signed in to change notification settings - Fork 0
/
async-iterator-input.mts
52 lines (46 loc) · 1.39 KB
/
async-iterator-input.mts
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
import example from '../lib/definition.cjs';
import {tqdm, TqdmOptions} from 'node-console-progress-bar-tqdm';
import * as timers from 'node:timers/promises';
type Item = {
value: number;
valueStr: string;
};
class It implements AsyncIterator<Item>, AsyncIterable<Item> {
public static readonly total = 250;
private val = 0;
[Symbol.asyncIterator]() {
return this;
}
async next(): Promise<IteratorResult<Item>> {
const curVal = this.val++;
const done = curVal >= It.total;
return {
done,
value: {
value: this.val,
valueStr: this.val.toString(),
},
};
}
}
export default example({
title: 'Iteration over async iterator',
description: 'Example with AsyncIterator and AsyncIterable as input',
tags: ['TS', 'AsyncIterable', 'async', 'for/await', 'color', 'styling'],
file: import.meta.url,
async run() {
const opts: TqdmOptions = {
total: It.total,
description: 'Async iterator',
progressSymbol: '|',
progressBraces: ['→', '←'],
progressColor: 'cyan',
};
const res: Item[] = [];
for await (const x of tqdm(new It(), opts)) {
res.push(x);
await timers.setTimeout(8);
}
console.log('Result length:', res.length);
},
});