- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlazy-man.ts
45 lines (39 loc) · 1.06 KB
/
lazy-man.ts
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
/**
* @description lazy man
* @author tangc1
* @date 2022-04-23 12:47:03
*/
classLazyMan{
privatename: string
privatetasks: Function[]=[]// 任务列表
constructor(name: string){
this.name=name
setTimeout(()=>{
this.next()
});
}
privatenext(){
consttask=this.tasks.shift()// 取出第一个task
if(task)task()
}
eat(foot: string){
consttask=()=>{
console.info(`${this.name} eat ${foot}`);
this.next()// 立即执行下一个任务
}
this.tasks.push(task)
returnthis// 链式调用
}
sleep(second: number){
consttask=()=>{
setTimeout(()=>{
console.info(`${this.name} sleep ${second}秒`);
this.next()// second秒之后执行下一个任务
},second*1000);
}
this.tasks.push(task)
returnthis// 链式调用
}
}
constme=newLazyMan('zhangsan')
me.eat('apple').eat('banana').sleep(2).eat('rice')