Skip to content
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

Unhandled promise rejection if promise rejects before min delay has passed #25

Open
joukosaastamoinen opened this issue Apr 14, 2023 · 4 comments

Comments

@joukosaastamoinen
Copy link

joukosaastamoinen commented Apr 14, 2023

Running this program (with node test.mjs):

// test.mjs
import pMinDelay from "p-min-delay";

pMinDelay(
  new Promise((_, reject) => {
    // Reject the promise _before_ the min delay.
    // If you reject _after_ the min delay (with a value greater than 200), then the program will not crash.
    setTimeout(reject, 100) 
  }),
  200
).catch(() => {});

crashes with this log output:

node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);
          ^

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "undefined".] {
  code: 'ERR_UNHANDLED_REJECTION'
}

The promise rejection should not go unhandled and the program should not crash, because there is a .catch() after the pMinDelay call.

This bug was likely introduced in this PR: #23

@sindresorhus
Copy link
Owner

// @fisker

@faizanu94
Copy link

Hello @joukosaastamoinen - to ensure clear understanding, could you kindly clarify the expected behavior following Promise.reject()? If feasible, please provide a test case to further illustrate this, as once the promise is rejected, control should transition to the catch block

@fisker
Copy link
Contributor

fisker commented Apr 16, 2023

I believe this case already tested

p-min-delay/test.js

Lines 29 to 33 in 3ccf4d5

test('minimum delay applies to rejection too', async t => {
const end = timeSpan();
await pMinDelay(Promise.reject(), 100).catch(() => {});
t.true(inRange(end(), {start: 70, end: 130}));
});

@joukosaastamoinen
Copy link
Author

Hello @joukosaastamoinen - to ensure clear understanding, could you kindly clarify the expected behavior following Promise.reject()? If feasible, please provide a test case to further illustrate this, as once the promise is rejected, control should transition to the catch block

The expected behavior would be for the program not to crash and not to produce any error log output.

I changed the code snippet in the test description not to use Promise.reject() and added some comments to it. Maybe it is easier to understand the problem now.

I noticed that there is a test case for this (as @fisker also pointed out), but for some reason, it does not fail, crash nor produce any error log output. But if you try running the code snippet that I provided, it will crash when it clearly shouldn't. So I don't know how to write a test case for this (yet).

Thank you for taking the time to look into this and sorry for the long delay in responding to your messages. I was on holiday.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants