denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
97.28k stars 5.36k forks source link

node:timers/promises setInterval not working #26499

Open jefbarn opened 1 week ago

jefbarn commented 1 week ago

Version: Deno 2.0.2

Example straight from the docs (https://docs.deno.com/api/node/timers/promises/~/setInterval)

import {
  setInterval,
} from 'node:timers/promises';

const interval = 100;
for await (const startTime of setInterval(interval, Date.now())) {
  const now = Date.now();
  console.log(now);
  if ((now - startTime) > 1000)
    break;
}
console.log(Date.now());
error: Uncaught (in promise) TypeError: The "callback" argument must be of type function. Received type number (100)
    at __node_internal_ (ext:deno_node/internal/validators.mjs:283:13)
    at setInterval (node:timers:35:3)
    at ext:deno_node/internal/util.mjs:97:15
    at new Promise (<anonymous>)
    at setInterval (ext:deno_node/internal/util.mjs:82:12)
cu8code commented 1 week ago

Hi @bartlomieju @littledivy, this is my first time contributing to Deno. I’ve successfully built the project, And I’m encountering the same error when trying to run the above code with the new build. Could someone point me in the right direction to resolve this issue? I’d really appreciate the help! Thanks in advance!

Gaurav23V commented 1 week ago

@cu8code I think deno node compatibility layer might be incorrectly resolving the node:times/promises to the traditional timers module since: Traditional: setInterval(callback, delay, ...args) Promises: setInterval([delay[, value[, options]]])

@bartlomieju @littledivy I would also like to look at the issue further and work on a fix if you could assign it to me.

marvinhagemeister commented 1 week ago

For folks wanting to tackle this: The issue is here where we're trying to promisify the existing setInterval implementation, whereas in node the promisified variant is a completely different implementation with different arguments, etc.

https://github.com/denoland/deno/blob/5389972ba5037b1ed48da11506f6798deec2b48e/ext/node/polyfills/timers.ts#L95

cu8code commented 6 days ago

@marvinhagemeister thanks will look into it