// Created on savesnippets.com ยท https://savesnippets.com/ME3ekxwhqAQHLg function createQueue() { let tail = Promise.resolve(); return { add(task) { const result = tail.then(() => task()); tail = result.catch(() => {}); // keep queue running on error return result; }, }; } // Usage const q = createQueue(); q.add(() => fetch('/api/step1').then((r) => r.json())).then(console.log); q.add(() => fetch('/api/step2').then((r) => r.json())).then(console.log); // step2 only starts after step1 finishes