From f73692e13df3c386824eb2a631ceaf594d194d23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alfredo=20Gonz=C3=A1lez?= Date: Thu, 14 Nov 2024 17:39:22 -0300 Subject: [PATCH 1/3] doc: add esm examples to node:timers --- doc/api/timers.md | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/doc/api/timers.md b/doc/api/timers.md index 83edc1d73b0e77..3def881f7ae027 100644 --- a/doc/api/timers.md +++ b/doc/api/timers.md @@ -292,7 +292,23 @@ returned Promises will be rejected with an `'AbortError'`. For `setImmediate()`: -```js +```mjs +import { setImmediate as setImmediatePromise } from 'node:timers/promises'; + +const ac = new AbortController(); +const signal = ac.signal; + +setImmediatePromise('foobar', { signal }) + .then(console.log) + .catch((err) => { + if (err.name === 'AbortError') + console.error('The immediate was aborted'); + }); + +ac.abort(); +``` + +```cjs const { setImmediate: setImmediatePromise } = require('node:timers/promises'); const ac = new AbortController(); @@ -310,7 +326,23 @@ ac.abort(); For `setTimeout()`: -```js +```mjs +import { setTimeout as setTimeoutPromise } from 'node:timers/promises'; + +const ac = new AbortController(); +const signal = ac.signal; + +setTimeoutPromise(1000, 'foobar', { signal }) + .then(console.log) + .catch((err) => { + if (err.name === 'AbortError') + console.error('The timeout was aborted'); + }); + +ac.abort(); +``` + +```cjs const { setTimeout: setTimeoutPromise } = require('node:timers/promises'); const ac = new AbortController(); From 503f80f2acf04921f4a6c2c4fac6b9d88e9a798c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alfredo=20Gonz=C3=A1lez?= <12631491+mfdebian@users.noreply.github.com> Date: Mon, 18 Nov 2024 19:42:58 -0300 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Antoine du Hamel --- doc/api/timers.md | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/api/timers.md b/doc/api/timers.md index 3def881f7ae027..229fa970b2aabc 100644 --- a/doc/api/timers.md +++ b/doc/api/timers.md @@ -298,6 +298,7 @@ import { setImmediate as setImmediatePromise } from 'node:timers/promises'; const ac = new AbortController(); const signal = ac.signal; +// We do not `await` the promise so `ac.abort()` is called concurrently. setImmediatePromise('foobar', { signal }) .then(console.log) .catch((err) => { From d8c341a4047a7657de8c7c8642b7fbea7849a6b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alfredo=20Gonz=C3=A1lez?= <12631491+mfdebian@users.noreply.github.com> Date: Mon, 18 Nov 2024 20:16:56 -0300 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Antoine du Hamel --- doc/api/timers.md | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/api/timers.md b/doc/api/timers.md index 229fa970b2aabc..9c4ca2ea17d263 100644 --- a/doc/api/timers.md +++ b/doc/api/timers.md @@ -333,6 +333,7 @@ import { setTimeout as setTimeoutPromise } from 'node:timers/promises'; const ac = new AbortController(); const signal = ac.signal; +// We do not `await` the promise so `ac.abort()` is called concurrently. setTimeoutPromise(1000, 'foobar', { signal }) .then(console.log) .catch((err) => {