//@ts-check // Helpers for a tracked promise land // by Humans for All // /** * @typedef {(resolve: (value: any) => void, reject: (reason?: any) => void) => void} PromiseExecutor */ /** * Eval which allows promises generated by the evald code to be tracked. * @param {string} codeToEval */ export async function evalWithPromiseTracking(codeToEval) { const _Promise = globalThis.Promise; const _fetch = globalThis.fetch /** @type {any[]} */ const trackedPromises = []; const Promise = function ( /** @type {PromiseExecutor} */ executor) { console.info("WW:PT:Promise") const promise = new _Promise(executor); trackedPromises.push(promise); promise.then = function (...args) { console.info("WW:PT:Then") const newPromise = _Promise.prototype.then.apply(this, args); trackedPromises.push(newPromise); return newPromise; }; promise.catch = function (...args) { console.info("WW:PT:Catch") const newPromise = _Promise.prototype.catch.apply(this, args); trackedPromises.push(newPromise); return newPromise; }; return promise; }; Promise.prototype = _Promise.prototype; Object.assign(Promise, _Promise); const fetch = function(/** @type {any[]} */ ...args) { console.info("WW:PT:Fetch") const fpromise = _fetch(args); trackedPromises.push(fpromise) return fpromise; } eval(codeToEval); //await Promise(resolve=>setTimeout(resolve, 0)); //return _Promise.allSettled(trackedPromises); return _Promise.all(trackedPromises); }