export type TaskHandler = () => T | PromiseLike export type Queue = { push: (task: TaskHandler) => Promise readonly length: number } export const makeTaskQueue = (): Queue => { let pending: Promise = Promise.resolve() let count: number = 0 const run = async (task?: TaskHandler) => { count++ try { await pending; } finally { count-- return Promise.resolve().then(task); } } return { push: (task: TaskHandler) => pending = run(task), length: count, } } export default makeTaskQueue