Skip to content

Commit c155bd3

Browse files
committed
SimpleChatTC:Ensure fetch's promise chain is also trapped
Dont forget to map members of got entity from fetch to things from saved original promise, bcas remember what is got is a promise. also add some comments around certain decisions and needed exploration
1 parent 788a9e4 commit c155bd3

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

tools/server/public_simplechat/xpromise.mjs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//@ts-check
22
// Helpers for a tracked promise land
3+
// Traps regular promise as well as promise by fetch
34
// by Humans for All
45
//
56

@@ -51,14 +52,36 @@ export async function evalWithPromiseTracking(codeToEval) {
5152
// @ts-ignore
5253
const fpromise = _fetch(args);
5354
trackedPromises.push(fpromise)
55+
56+
// @ts-ignore
57+
fpromise.then = function (...args) {
58+
console.info("WW:PT:FThen")
59+
const newPromise = _Promise.prototype.then.apply(this, args);
60+
trackedPromises.push(newPromise);
61+
return newPromise;
62+
};
63+
64+
fpromise.catch = function (...args) {
65+
console.info("WW:PT:FCatch")
66+
const newPromise = _Promise.prototype.catch.apply(this, args);
67+
trackedPromises.push(newPromise);
68+
return newPromise;
69+
};
70+
5471
return fpromise;
5572
}
5673

74+
fetch.prototype = _fetch.prototype;
75+
Object.assign(fetch, _fetch);
76+
5777
//let tf = new Function(codeToEval);
5878
//await tf()
5979
await eval(`(async () => { ${codeToEval} })()`);
6080

81+
// Should I allow things to go back to related event loop once
6182
//await Promise(resolve=>setTimeout(resolve, 0));
62-
//return _Promise.allSettled(trackedPromises);
83+
84+
// Need and prefer promise failures to be trapped using reject/catch logic
85+
// so using all instead of allSettled.
6386
return _Promise.all(trackedPromises);
6487
}

0 commit comments

Comments
 (0)