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

timers: allow promisified timeouts/immediates to be canceled #33833

Closed
wants to merge 1 commit into from

Conversation

jasnell
Copy link
Member

@jasnell jasnell commented Jun 10, 2020

Using the new experimental AbortController...

const { promisify } = require('util');
const sleep = promisify(setTimeout);

const ac = new AbortController();
const signal = ac.signal;

sleep(1000, undefined, { signal });

ac.abort(); // cancels the setTimeout and rejects the Promise
const { promisify } = require('util');
const immediate = promisify(setImmediate);

const ac = new AbortController();
const signal = ac.signal;

immediate(undefined, { signal });

ac.abort(); // cancels the setImmediate and rejects the Promise

This will necessarily be experimental for as long as AbortController is experimental.

Signed-off-by: James M Snell jasnell@gmail.com

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

@nodejs-github-bot nodejs-github-bot added the timers Issues and PRs related to the timers subsystem / setImmediate, setInterval, setTimeout. label Jun 10, 2020
@mcollina
Copy link
Member

I like this. However I think it's time we design a few new apis for setImmediate and setTimeout that are both promisified from the start and abortable.

I do promisify(setTimeout) too many times already.

On a technical side this would have the benefit of avoiding to call promisify to change the AbortController

@jasnell
Copy link
Member Author

jasnell commented Jun 10, 2020

I've been considered exporting the promisified versions of setTimeout and setImmediate as a separate namespace off timers such that we could do...

const {
  setTimeout,
  setImmediate
} = require('timers/promises');

I'm also considering a promisified version of setInterval that returns an async iterator such that...

const {
  setInterval,
} = require('timers/promises');

(async () => {
  for await (const n of setInterval(1000)) {
    console.log('tick...')
  }
})()

We also need to consider how to ref/unref promisified timers/intervals/immediates.

lib/timers.js Outdated Show resolved Hide resolved
lib/timers.js Outdated Show resolved Hide resolved
@jasnell
Copy link
Member Author

jasnell commented Jun 16, 2020

@mcollina ... I will introduce require('timers/promises') in a separate PR

@jasnell jasnell added the semver-minor PRs that contain new features and should be released in the next minor version. label Jun 16, 2020
@nodejs-github-bot
Copy link
Collaborator

@jasnell jasnell added the experimental Issues and PRs related to experimental features. label Jun 16, 2020
@jasnell jasnell requested review from addaleax and devsnek June 16, 2020 19:29
lib/timers.js Show resolved Hide resolved
lib/timers.js Outdated Show resolved Hide resolved
Using the new experimental AbortController...

Signed-off-by: James M Snell <jasnell@gmail.com>
Copy link
Member

@addaleax addaleax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice :)

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

jasnell added a commit that referenced this pull request Jun 18, 2020
Using the new experimental AbortController...

Signed-off-by: James M Snell <jasnell@gmail.com>

PR-URL: #33833
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
@jasnell
Copy link
Member Author

jasnell commented Jun 18, 2020

Landed in bfbdc84

@codebytere
Copy link
Member

Marking dont-land since #33527 is semver-major

targos pushed a commit to targos/node that referenced this pull request Apr 24, 2021
Using the new experimental AbortController...

Signed-off-by: James M Snell <jasnell@gmail.com>

PR-URL: nodejs#33833
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
targos pushed a commit to targos/node that referenced this pull request Apr 26, 2021
Using the new experimental AbortController...

Signed-off-by: James M Snell <jasnell@gmail.com>

PR-URL: nodejs#33833
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
targos pushed a commit to targos/node that referenced this pull request Apr 30, 2021
Using the new experimental AbortController...

Signed-off-by: James M Snell <jasnell@gmail.com>

PR-URL: nodejs#33833
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
targos pushed a commit that referenced this pull request Apr 30, 2021
Using the new experimental AbortController...

Signed-off-by: James M Snell <jasnell@gmail.com>

PR-URL: #33833
Backport-PR-URL: #38386
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
@danielleadams danielleadams mentioned this pull request May 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
experimental Issues and PRs related to experimental features. semver-minor PRs that contain new features and should be released in the next minor version. timers Issues and PRs related to the timers subsystem / setImmediate, setInterval, setTimeout.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants