-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise.js
49 lines (46 loc) · 1.19 KB
/
promise.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
const path = require("path");
const fs = require("fs");
const readFile = function(filePath) {
return new Promise((resolve, reject) => {
fs.readFile(filePath, "utf-8" ,(err, data) => {
if(err) {
return reject(err);
};
resolve(data);
})
})
}
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)
};
const iter = generator();
function runGen(gen) {
const iter = gen();
function next(data) {
const result = iter.next(data);
if(result.done) {
return result.value;
};
result.value.then(res => {
next(res);
})
};
next();
};
runGen(generator);
function* gen() {
const a = yield 4;
console.log(a);
}
const it = gen();
console.log(it.next());
it.next(3)