当前位置:网站首页>JS chain call sleep function ------ "autumn trick punch in Chapter 2"
JS chain call sleep function ------ "autumn trick punch in Chapter 2"
2022-07-19 12:12:00 【Shadow wind Mo】
Implement a function , First, the output hello 5s Then output world as follows :
let person = new Person()
person.say('hello')
.sleep(5000)
.say('world')
.done()
Ideas :
- Use one task Tasks used for array access
- done Function is aysnc function
- for Loop handle tash Array await Function execution per function
class Persion {
constructor() {
this.task = [];
}
say(msg) {
this.task.push(
() =>
new Promise((reslove) => {
console.log(msg);
reslove();
})
);
return this;
}
sleep(timeout) {
this.task.push(
() =>
new Promise((reslove) => {
setTimeout(() => reslove(''), timeout);
})
);
return this;
}
async done() {
for (let i = 0; i < this.task.length; i++) {
await this.task[i].call(null);
}
}
}
let person = new Persion();
person.say('hello').sleep(5000).say('world').done();
Welcome to add qq Group communication : 623354172
边栏推荐
- MATLAB(4)函数及文件
- HCIP (7)
- Opencv draw a black rectangle and write the serial number
- Ten minutes from pytorch to mxnet
- [unity technology accumulation] realize the mouse line drawing function &linerenderer
- STL string input / output overload
- Genesis与BlueRun Ventures展开深度交流
- Scrapy关键词 爬虫的简单实现(以新华网和人民网为例)
- 谷歌开发者社区分享——Flutter动画的分享已发布
- 人大、微软等提出InclusiveFL:异构设备上的包容性联邦学习
猜你喜欢

NAT technology and NAT alg

2022-07-07:Spire.Office 7.7.2 for net 闪亮登场

MGRE 环境下配置OSPF实验

C language drawing example - flower pattern

Round table record: fireside dialogue -- how to realize innovation in Web3

『牛客|每日一题』有效括号序列

Nature子刊 | 地下水固碳速率与寡营养海洋系统固碳速率相近

QT learning diary 17 - QT database

Kunlunbase online meetup is waiting for you~

Dual machine hot standby of Huawei firewall (NGFW)
随机推荐
How to build dashboard and knowledge base in double chain note taking software? Take the embedded widget library notionpet as an example
Send blocking, receive blocking
Application of semi supervised learning in malware traffic detection
李宏毅《机器学习》|1. Introduction of this course(机器学习介绍)
C language drawing example - palette
Tidb memory control document
LeetCode_ 216_ Combined sum III
Scrapy关键词 爬虫的简单实现(以新华网和人民网为例)
Two misunderstandings of digital transformation
PPPoE拨号上网
Chinese garbled code problem in reading binary files
The underlying principle of file operation (inode and hard and soft links, time attributes of files)
STL string输入输出重载
NPC, Microsoft, etc. proposed inclusivefl: inclusive federal learning on heterogeneous devices
核芯基站_启动CPA文件报错‘No gateways configured’
Microcomputer principle and technology Interface Experiment four subroutines and interrupt experiment
STL string input / output overload
Flink
LeetCode_ 77_ combination
百度文档翻译api