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)
There seems to be no reliable way right know to know when a promise is rejected but not handled. The engine exposes a
JS_SetHostPromiseRejectionTrackerto know when a promise is rejected. It provides a parameteris_handledthat should tell whether the promise was handled or not. However in the following case, the callback will be called with theis_handledparameter set to 0, even if the JS code is actually handling the promise:This makes it impossible to make a distinction between the code above, and this code:
or even this code:
In all 3 cases,
is_handledis 0, although in the former example, the callback is called twice: once with0, once with1, but I don't see a way to know whether a second callback should be expected.In
JavaScriptCore, the callback given toJSGlobalContextSetUnhandledRejectionCallbackis only called in the last two examples. (https://github.com/WebKit/WebKit/blob/main/Source/JavaScriptCore/API/JSContextRefPrivate.h#L139)