-
Notifications
You must be signed in to change notification settings - Fork 0
/
test2.js
54 lines (45 loc) · 1.11 KB
/
test2.js
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
"use strict";
let fs = require("fs");
function thread(fn) {
console.log("thread(): 1");
console.log("fn: " + fn);
let gen = fn();
console.log("thread(): 2");
console.log("gen: " + gen);
function next(err, res) {
console.log("next(): 1");
console.log("err: " + err);
console.log("res: " + res);
let ret = gen.next(res);
console.log("next(): 2");
console.log("ret: " + ret);
if (ret.done) return;
console.log("next(): 3");
ret.value(next);
console.log("next(): 4");
}
console.log("thread(): 3");
next();
console.log("thread(): 4");
}
function read(path) {
console.log("read(): 1");
console.log("path: " + path);
return function (done) {
console.log("read-anonymous(): 1");
console.log("done: " + done);
fs.readFile(path, "utf8", done);
console.log("read-anonymous(): 2");
}
}
console.log("global: 1");
thread(function* () {
console.log("anonymous-generator(): 1");
var a = yield read("1.txt");
console.log("anonymous-generator(): 2");
console.log("a: " + a);
var b = yield read("2.txt");
console.log("anonymous-generator(): 3");
console.log("b: " + b);
});
console.log("global: 2");