export default function createMutex() { let isRunning = false const tasks: any[] = [] const next = async () => { if (!isRunning) { while (tasks.length) { const task = tasks.shift() isRunning = true await task().catch(() => { // do nothing }) isRunning = false } } } const runExclusively = async (task) => { const prms = new Promise((resolve, reject) => { tasks.push(async () => { try { resolve(await task()) } catch (e) { reject(e) } }) }) next() return prms } return { next, runExclusively } }