sindresorhus/p-wait-for

TimeoutError not terminating process as expected

Closed this issue · 3 comments

I am using p-wait-for version 2.0.0.

I'm seeing strange behavior when p-wait-for times out. When I run this example code it reports that a timeout happens, but the node process does not terminate. I need to hit Control-C to terminate the process.

const pWaitFor = require('p-wait-for');

(async () =>
  pWaitFor(() => false, { timeout: 1000 })
    .then(() => console.log('Magically got here?'))
    .catch(console.log)
)();

Result of running:

time node p-wait-for-test.js
{ TimeoutError: Promise timed out after 1000 milliseconds
    at Timeout.setTimeout [as _onTimeout] (./node_modules/p-timeout/index.js:27:54)
    at ontimeout (timers.js:482:11)
    at tryOnTimeout (timers.js:317:5)
    at Timer.listOnTimeout (timers.js:277:5) name: 'TimeoutError' }
^C
node p-wait-for-test.js  0.07s user 0.02s system 3% cpu 2.752 total

It times out after 1000 ms as expected, but the process continued until I hit Control-C after 2.752 seconds.

I would expect behavior similar to this next example, which terminates the process without my intervention:

(async () =>
  Promise.reject(new Error('Failure'))
)();

It looks like the check is not being cancelled after a timeout occurs. This demonstrates the issue:

const pWaitFor = require('p-wait-for');

(async () =>
  pWaitFor(
    () => {
      console.log('performing check');
      return false;
    },
    {
      interval: 200,
      timeout: 1000
    }
  )
    .then(() => console.log('Magically got here?'))
    .catch(console.log)
)();

Output:

$ time node p-wait-for-test.js
performing check
performing check
performing check
performing check
performing check
{ TimeoutError: Promise timed out after 1000 milliseconds
    at Timeout.setTimeout [as _onTimeout] (./node_modules/p-timeout/index.js:27:54)
    at ontimeout (timers.js:482:11)
    at tryOnTimeout (timers.js:317:5)
    at Timer.listOnTimeout (timers.js:277:5) name: 'TimeoutError' }
performing check
performing check
performing check
performing check
performing check
performing check
performing check
^C
node p-wait-for-test.js  0.06s user 0.02s system 3% cpu 2.384 total

I'll try to get a PR fixing this pulled together this afternoon.

@yjpa7145 any luck with that PR?

Yes, it's here: #7