Skip to content

timers: allow promisified timeouts/immediates to be canceled

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

Merge request reports

Loading