任务调度器

class TaskManager{
	list = [];
	count= 0;
	constructor(num){
		this.num = num
	}
	async addTask(fn){
		this.count>=this.num?await new Promise(resolve=>{this.list.push(resolve)}):""
		this.count++
		var result = await fn()
		this.count--
		if(this.list.length>0){
			this.list.shift()()
		}
		return result
	}
}
const tm = new TaskManager(1)
tm.addTask(() => Promise.resolve(10)).then(data => { console.log(data) }) // 10
tm.addTask(() => Promise.resolve(20)).then(data => { console.log(data) }) // 20

LazyMan–sleep

class LazyMan{
	constructor(){
		this.queue = [start]
		function start(resolve){
			resolve()
		}
		setTimeout(()=>{
			let start = Promise.resolve()
			for(let i = 0 ; i<this.queue.length;i++){
				start = start.then(()=>new Promise(this.queue[i]))
			}
		})
	}
	addSleep(time){
		function addSleep(resolve){
			setTimeout(function()){
				resolve()
			},time)
		}
		this.queue.push(addSleep)
		return this
	}
	eatFood(){
		function eatFun(resolve){
			resolve()
		}
		this.queue.push(eatFun)
		return this
	}
}
let man = new LazyMan()
man.addSleep(1000).eatFood('haobao').addSleep(1000).eatFood('gg')

delay(console.log, 3, 1000)(‘hello’)

function delay(fn,nums,times){
	return async function(content){
		for(let i = 0 ; i<nums;i++){
			await new Promise(resolve=>{
				setTimeout(()=>{
					fn.call(this,conent)
					resolve()
				},times)
			})
		}
	}
}
delay(console.log, 3, 4000)('hello')