/** * @description 链式调用的方法 * @param {*} taskIs * @example * arrange('arrage').waitFirst(2).do('吃西瓜').do('吃西瓜2').execute() * @returns {execute, do, wait, waitFirst} */
export const arrange = (taskIs) => { const tasks = [] tasks.push(() => { console.log(`${taskIs} IS UNDEFINED`); }) async function execute(){ for(const t of tasks){ await t() } } function doSomething(someThing){ tasks.push(() => { console.log(`start to ${someThing}`); }) return this } function wait(dur){ tasks.push(() => new Promise(resolve => { setTimeout(resolve, dur*1000); })) return this } // 先等待具体时间在执行 function waitFirst(dur){ tasks.unshift(() => new Promise(resolve => { setTimeout(resolve, dur*1000); })) return this } return { execute, do:doSomething, wait, waitFirst } }
标签:function,do,tasks,函数,resolve,编写,dur,arrange From: https://www.cnblogs.com/xuhuang/p/17261347.html