-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLazyMan.js
49 lines (43 loc) · 917 Bytes
/
LazyMan.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
class LazyManProto {
constructor(name) {
this.name = name;
this.tasks = [];
this.tasks.push(() => {
console.log('Hi, this is ' + this.name);
this.next();
});
setTimeout(() => {
this.next();
}, 0);
}
next() {
let fn = this.tasks.shift();
fn && fn();
}
sleep(time) {
this.tasks.push(() => {
setTimeout(() => {
console.log('wait! i sleep ' + time + 'ms');
this.next();
}, time);
});
return this;
}
sleepFirst(time) {
this.tasks.unshift(() => {
setTimeout(() => {
console.log('sleep first ' + time + 'ms');
this.next();
}, time);
});
return this;
}
eat(food) {
this.tasks.push(() => {
console.log('eat' + food);
this.next();
});
return this;
}
}
var LazyMan = name => new LazyManProto(name);