Skip to content

‘timersPromises.setInterval()’ doesn’t keep the event loop active #45224

Closed
@leafac

Description

@leafac

Version

v18.12.0

Platform

Darwin leafac--mac-mini.lan 21.6.0 Darwin Kernel Version 21.6.0: Mon Aug 22 20:20:05 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T8101 arm64

Subsystem

No response

What steps will reproduce the bug?

As far as I understand from having read the documentation, timersPromises.setInterval() was supposed to keep the event loop active, in much the same way that other timers including setInterval() do. That’s why one of the possible options is ref.

But that doesn’t seem to be working.

Here’s an example program:

// index.mjs
import timers from "node:timers/promises";
timers.setInterval(1000);

This program exits immediately. I tried changing the ref option, including setting it to true explicitly, to no effect.

Here’s a similar program using setInterval(), which does keep the event loop active and does not terminate right away:

// index.mjs
setInterval(() => {});

This may seem like a silly program, but sometimes it’s actually useful to have a dummy timer like that to exert more control over the event loop and when it terminates.

How often does it reproduce? Is there a required condition?

No response

What is the expected behavior?

No response

What do you see instead?

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions