forked from nodejs/node-addon-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
49 lines (44 loc) · 1.29 KB
/
index.js
File metadata and controls
49 lines (44 loc) · 1.29 KB
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
const { AsyncIteratorExample } = require('bindings')('example');
async function main(from, to) {
const iterator = new AsyncIteratorExample(from, to);
for await (const value of iterator) {
console.log(value);
}
}
/*
// The JavaScript equivalent of the node-addon-api C++ code for reference
async function main(from, to) {
class AsyncIteratorExample {
constructor(from, to) {
this.from = from;
this.to = to;
}
[Symbol.asyncIterator]() {
return {
current: this.from,
last: this.to,
next() {
return new Promise(resolve => {
setTimeout(() => {
if (this.current <= this.last) {
resolve({ done: false, value: this.current++ });
} else {
resolve({ done: true });
}
}, 1000)
});
}
}
}
}
const iterator = new AsyncIteratorExample(from, to);
for await (const value of iterator) {
console.log(value);
}
}
*/
main(0, 5)
.catch(e => {
console.error(e);
process.exit(1);
});