-
-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
promiseStateAsync
swallows unhandled rejections if they are rejected after being observed by the Promise.race
#12
Comments
Thanks for reporting. I'm not sure how to fix this, but I have at least added a failing test: 4634d7b |
Hello again, I have a solution for this Issue: export async function promiseStateAsync(promise) {
if (!(typeof promise === 'object' && typeof promise.then === 'function')) {
throw new TypeError(`Expected a promise, got ${typeof promise}`);
}
let promiseState = 'pending';
let promiseSettled = false;
// `.finally` doesn't handle uncaught rejections
promise.finally(() => {
promiseSettled = true;
});
// We wait an event loop for `.finally` to resolve.
// If it doesn't resolve by then that means the promise
// is still `pending`.
await new Promise(resolve => {
if (typeof setImmediate === 'function') {
setImmediate(resolve);
} else {
setTimeout(resolve);
}
});
// If the passed function to `.finally` is not
// invoked in an event loop that means the promise
// is not yet settled, so we return `pending`.
if (!promiseSettled) {
return promiseState;
}
promise.then(
() => {
promiseState = 'fulfilled';
},
() => {
promiseState = 'rejected';
},
);
// We wait for 1 event loop for `.then` to resolve
// knowing that the promise is settled to extract
// the status of the promise
await new Promise(resolve => {
if (typeof setImmediate === 'function') {
setImmediate(resolve);
} else {
setTimeout(resolve);
}
});
return promiseState;
} I ran the repo tests successfully on:
Win 11
Results: I also ran the following assertions in the browsers: (async () => {
console.assert(await promiseStateAsync(Promise.resolve()) === "fulfilled");
console.assert(await promiseStateAsync(Promise.reject("Dino 1")) === "rejected");
console.assert(await promiseStateAsync(new Promise(() => {})) === "pending");
const {promise: promiseThatWillReject, reject } = Promise.withResolvers();
console.assert(await promiseStateAsync(promiseThatWillReject) === "pending");
setTimeout(async () => {
reject("Dino 2");
console.assert(await promiseStateAsync(promiseThatWillReject) === "rejected");
}, 150);
const {promise: promiseThatWillResolve, resolve} = Promise.withResolvers();
console.assert(await promiseStateAsync(promiseThatWillResolve) === "pending");
setTimeout(async () => {
resolve();
console.assert(await promiseStateAsync(promiseThatWillResolve) === "fulfilled");
}, 150);
})(); Tested on the following browsers:
Win 11
Results: The downside is that the state will take an extra event loop run to be returned (if the promise is not Also this solution doesn't need to pass 1 to the The stackoverflow answers that led me to |
It turns out my solution is flawed because TL;DR: My solution throws an unhandled rejection even when the promise was handled. |
Using the following snippet,
promiseStateAsync
will swallow unhandled rejections if the promise is rejected after being inspected bypromiseStateAsync
Note: in the browser I used the following events to check if the unhandled rejection event is raised, which it wasn't:
This behavior is seen in both Node and browsers
I have tested it on the following platforms:
Win11 Chrome Version 122.0.6261.129
Win11 Firefox Version 123.0.1
macOS Sonoma 14.4 Chrome Version 122.0.6261.129
macOS Sonoma 14.4 Firefox Version 123.0.1
macOS Sonoma 14.4 Safari Version 17.4 (19618.1.15.11.12)
Node versions v18.16.1 and v20.11.0 on both Win11 and macOS Sonoma 14.4
The text was updated successfully, but these errors were encountered: