-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple.js
90 lines (87 loc) · 2.25 KB
/
simple.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const fs = require("fs");
const path = require('path');
const readFile = function (filePath) {
return new Promise((resolve, reject) => {
fs.readFile(filePath, "utf-8", (err, data) => {
if (!err) {
resolve(data);
} else {
reject(err)
}
})
})
};
function* generator() {
const file1 = yield readFile(path.join(__dirname, '1.txt'));
console.log(file1);
const file2 = yield readFile(path.join(__dirname, '2.txt'));
console.log(file2);
const file3 = yield readFile(path.join(__dirname, '1.txt'));
console.log(file3);
const file4 = yield readFile(path.join(__dirname, '2.txt'));
console.log(file4);
const file5 = yield readFile(path.join(__dirname, '1.txt'));
console.log(file5);
};
function run(gen) {
const it = gen();
function next(data) {
const result = it.next(data);
if (result.done) {
return result.value
}
result.value.then(data => {
next(data);
})
};
next();
};
run(generator)
// 手动执行
// const iter = generator();
// iter.next().value.then(res => {
// iter.next(res).value.then(res => {
// iter.next(res);
// });
// });
function thunk(fn) {
return function() {
const args = Array.prototype.slice.call(arguments);
const ctx = this;
return function(cb) {
let called = false;
args.push(function() {
if(called) return;
called = true;
cb.apply(ctx, arguments)
});
return fn.apply(ctx, args);
}
}
}
const Thunk = function (fn) {
return function () {
const args = Reflect.apply(Array.prototype.slice, arguments, []);
return function (cb) {
var called;
args.push(function() {
if(called) return;
called = true;
cb.apply(this, arguments)
})
return fn.apply(this, args);
}
}
}
function test(name, age, callback) {
callback(name, age);
};
const testThunk = Thunk(test);
testThunk(1,2)((a, b) => console.log(a, b))
var obj = {
a: [1, 2, 3]
};
var c = obj.a;
const v = {};
v.c = c;
console.log(obj.a === v.c)