Skip to content

Instantly share code, notes, and snippets.

@alii
Created October 13, 2021 04:01
Show Gist options
  • Save alii/3c661677811c8e12d7b30910fd77eb95 to your computer and use it in GitHub Desktop.
Save alii/3c661677811c8e12d7b30910fd77eb95 to your computer and use it in GitHub Desktop.
Experimenting with Node `timers/promises`
import { setInterval } from "timers/promises";
const createCounter = (initial = 0) => {
let value = initial;
return () => ++value;
};
const start = Date.now();
for await (const incr of setInterval(1000, createCounter())) {
const i = incr();
console.log(i, Date.now() - start);
if (i >= 10) {
break;
}
}
console.log("Done!");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment