Skip to content

Instantly share code, notes, and snippets.

@Hahet
Hahet / chain.js
Last active November 16, 2020 07:07
// 实现带异步任务的链式调用:chain.eat().sleep(5).work().eat().work().sleep(10)
// 链式调用:函数都 return this
// 链式调用时(同步过程),会将每个任务(函数)放到 队列中(tasks)
// 使用 start 函数异步开始执行第一个任务( next 函数第一次调用) 每一个任务执行完就 再调用 next 函数,next 会取出下一个任务出来执行
class Chaos {
constructor() {
this.tasks = []
const fn = () => {
console.log('chain start')
this.next()