/** @param promises {Array>} */ export async function * onSettled(promises) { let wake; let wait = new Promise(_ => wake = _); let pending = promises.length; const queue = []; for (const p of promises) { Promise.allSettled([p]).then(([result]) => { queue.push(result); pending--; wake(); wait = new Promise(_ => wake = _); }); } while (pending > 0) { await wait; while (queue.length) { yield queue.shift(); } } }