-
Notifications
You must be signed in to change notification settings - Fork 457
/
index.js
83 lines (71 loc) · 1.77 KB
/
index.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
const fs = require('fs');
/**
* Promise 之前
*
* 如果使用回调函数实现多个异步并行执行,同一时刻获取最终结果
* 可以借助 发布订阅/观察者模式
*/
let pubsub = {
arry: [],
emit() {
this.arry.forEach(fn => fn());
},
on(fn) {
this.arry.push(fn);
}
}
let data = [];
pubsub.on(() => {
if(data.length === 3) {
console.log(data); //[ '22', 'Yvette', 'engineer' ];数组顺序随机
}
});
fs.readFile('./JS/Async/data/age.txt', 'utf-8', (err, value) => {
data.push(value);
pubsub.emit();
});
fs.readFile('./JS/Async/data/name.txt', 'utf-8', (err, value) => {
data.push(value);
pubsub.emit();
});
fs.readFile('./JS/Async/data/job.txt', 'utf-8', (err, value) => {
data.push(value);
pubsub.emit();
});
/**
* 将 fs.readFile 包装成promise接口
*/
function read(url) {
return new Promise((resolve, reject) => {
fs.readFile(url, 'utf8', (err, data) => {
if(err) reject(err);
resolve(data);
});
});
}
/**
* 使用 Promise
*
* 通过 Promise.all 可以实现多个异步并行执行,同一时刻获取最终结果的问题
*/
Promise.all([
read('./JS/Async/data/age.txt'),
read('./JS/Async/data/name.txt'),
read('./JS/Async/data/job.txt')
]).then(data => {
console.log(data); //[ '22', 'Yvette', 'engineer' ];数组顺序随机
}).catch(err => console.log(err));
/**
* 使用 Async/Await
*/
async function readAsync() {
let data = await Promise.all([
read('./JS/Async/data/age.txt'),
read('./JS/Async/data/name.txt'),
read('./JS/Async/data/job.txt')
]);
return data;
}
readAsync().then(data => {
console.log(data); //[ '22', 'Yvette', 'engineer' ];数组顺序随机
});