猿问
封装一个obj对象 实现 obj 链式调用,异步等待settimeOut之后继续调用
例:
obj.write("1").await(1000).write("2");
// 打印 1
// 等待 await 时长之后打印 2### 题目描述
有只小跳蛙
浏览 481
回答 2
2回答
跃然一笑
class Obj { constructor () { this.sleep = 0 } write (str) { if (this.sleep) { setTimeout(() => { console.log(str) }, this.sleep) } else { console.log(str) } return this } await (time) { this.sleep += time return this } } const obj = new Obj() obj.write('1').await(1000).write('2').await(3000).write(3)
0
0
0
互换的青春
function Delay() { this.queue = Promise.resolve(); } Delay.prototype.write = function() { var args = arguments; var _this = this; this.queue = this.queue.then(function() { console.log.apply(_this, args); }); return this; }; Delay.prototype.await = function(time) { this.queue = this.queue.then(function() { return new Promise(function(resolve) { setTimeout(resolve, time); }); }); return this; };var obj = new Delay(); obj.write("1").await(1000).write("2");
0
0
0
随时随地看视频
慕课网APP
相关分类
JavaScript
我要回答