|
1 | 1 | //@ts-check |
2 | 2 | // Helpers for a tracked promise land |
| 3 | +// Traps regular promise as well as promise by fetch |
3 | 4 | // by Humans for All |
4 | 5 | // |
5 | 6 |
|
@@ -51,14 +52,36 @@ export async function evalWithPromiseTracking(codeToEval) { |
51 | 52 | // @ts-ignore |
52 | 53 | const fpromise = _fetch(args); |
53 | 54 | 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 | + |
54 | 71 | return fpromise; |
55 | 72 | } |
56 | 73 |
|
| 74 | + fetch.prototype = _fetch.prototype; |
| 75 | + Object.assign(fetch, _fetch); |
| 76 | + |
57 | 77 | //let tf = new Function(codeToEval); |
58 | 78 | //await tf() |
59 | 79 | await eval(`(async () => { ${codeToEval} })()`); |
60 | 80 |
|
| 81 | + // Should I allow things to go back to related event loop once |
61 | 82 | //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. |
63 | 86 | return _Promise.all(trackedPromises); |
64 | 87 | } |
0 commit comments