SimpleChatTC:TrapPromise: log the trapping

also possible refinement wrt trapping, if needed, added as comment

all or allSettled to use or not is the question.

whether to wait for a round trip through the related event loop or
not is also a question.
This commit is contained in:
hanishkvc 2025-10-15 21:48:18 +05:30
parent 3d661793ef
commit 0241b7b469
2 changed files with 8 additions and 1 deletions

View File

@ -15,6 +15,7 @@ import * as xpromise from "./xpromise.mjs"
self.onmessage = async function (ev) {
console.info("DBUG:WW:OnMessage started...")
tconsole.console_redir()
try {
await xpromise.evalWithPromiseTracking(ev.data.code);
@ -23,4 +24,5 @@ self.onmessage = async function (ev) {
}
tconsole.console_revert()
self.postMessage({ id: ev.data.id, name: ev.data.name, data: tconsole.gConsoleStr})
console.info("DBUG:WW:OnMessage done")
}

View File

@ -13,22 +13,25 @@
* Eval which allows promises generated by the evald code to be tracked.
* @param {string} codeToEval
*/
export function evalWithPromiseTracking(codeToEval) {
export async function evalWithPromiseTracking(codeToEval) {
const _Promise = globalThis.Promise;
/** @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;
@ -42,5 +45,7 @@ export function evalWithPromiseTracking(codeToEval) {
eval(codeToEval);
//await Promise(resolve=>setTimeout(resolve, 0));
//return _Promise.allSettled(trackedPromises);
return _Promise.all(trackedPromises);
}