Voyz's Studio.

流程控制思想--链式调用的简单实现

字数统计: 129阅读时长: 1 min
2021/06/01 Share
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
/**
* Lazy man
*/
class Tom{
constructor(){
this.queue = []
setTimeout(()=>{
this.next();
},0)
}
next(){
let fun = this.queue.shift();
fun && fun();
}
eat(value){
let fun = ()=>{
console.log(value);
this.next();
}
this.queue.push(fun);
return this
}
sleepFirst(gap){
let fun = ()=>{
setTimeout(()=>{
this.next();
},gap)
}
this.queue.unshift(fun);
return this
}
sleep(gap){
let fun = ()=>{
setTimeout(()=>{
this.next();
},gap)
}
this.queue.push(fun);
return this
}
play(value){
let fun = ()=>{
console.log(value);
this.next();
}
this.queue.push(fun)
return this
}
}

new Tom().eat('apple').sleep(2000).play('soccer').sleepFirst(1000);
CATALOG