Skip to content

timers: introduce setInterval async iterator - #37153

Closed
Linkgoron wants to merge 1 commit into
nodejs:masterfrom
Linkgoron:timers-set-interval-async
Closed

Linkgoron wants to merge 1 commit into
nodejs:masterfrom
Linkgoron:timers-set-interval-async

Conversation

@Linkgoron

@Linkgoron Linkgoron commented Jan 31, 2021 •

Copy link
Copy Markdown
Contributor

Added setInterval async generator to timers\promises. Utilises async generators to provide an iterator compatible with
for await. This is a continuation of initial work by Fabian Cook #35841 (who is also marked as co-author in this PR). The async-generator throws an AbortError when aborted.

Note that there are some decisions I've made regarding (so-called) backpressure. If multiple iterations were completed before a value is yielded, the next iteration won't wait delay long, but yield the next value in the next tick. In addition, if there was backpressure before the controller was aborted, the generator will emit all of the passed intervals, and only then finish the iteration. I'd be happy to change this behavior if this is not what's expected (i.e. change the generator to stop producing immediately).

See the previous PR for more information and discussions.

Example:

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

const ac = new AbortController();
const { signal } = ac;
const interval = 10;
setTimeout(() => ac.abort(), 500);

let i=0;
for await (const value of setInterval(interval, undefined, { ref: false, signal })) {
  console.log(Date.now(), i);
  if(i++>=5) {
     break;
  }
}
  • 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

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

semver-minor PRs that contain new features and should be released in the next minor version. timers Issues and PRs related to timers, setImmediate(), setInterval(), and setTimeout().

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants