Description
There seems to be no reliable way right know to know when a promise is rejected but not handled. The engine exposes a JS_SetHostPromiseRejectionTracker
to know when a promise is rejected. It provides a parameter is_handled
that should tell whether the promise was handled or not. However in the following case, the callback will be called with the is_handled
parameter set to 0, even if the JS code is actually handling the promise:
new Promise((resolve, reject) => {
reject(42)
}).catch(() => {
})
This makes it impossible to make a distinction between the code above, and this code:
new Promise((resolve, reject) => {
resolve();
}).then(() => {
throw new Error('bad');
}
or even this code:
new Promise((resolve, reject) => {
reject(42);
});
In all 3 cases, is_handled
is 0, although in the former example, the callback is called twice: once with 0
, once with 1
, but I don't see a way to know whether a second callback should be expected.
In JavaScriptCore
, the callback given to JSGlobalContextSetUnhandledRejectionCallback
is only called in the last two examples. (https://github.com/WebKit/WebKit/blob/main/Source/JavaScriptCore/API/JSContextRefPrivate.h#L139)