Skip to content

Commit 41e3bcd

Browse files
mfdebianmarco-ippolito
authored andcommittedJan 24, 2025
doc: add esm examples to node:timers
PR-URL: #55857 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Jason Zhang <xzha4350@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent a17d9e1 commit 41e3bcd

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed
 

‎doc/api/timers.md

+36-2
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,24 @@ returned Promises will be rejected with an `'AbortError'`.
288288

289289
For `setImmediate()`:
290290

291-
```js
291+
```mjs
292+
import { setImmediate as setImmediatePromise } from 'node:timers/promises';
293+
294+
const ac = new AbortController();
295+
const signal = ac.signal;
296+
297+
// We do not `await` the promise so `ac.abort()` is called concurrently.
298+
setImmediatePromise('foobar', { signal })
299+
.then(console.log)
300+
.catch((err) => {
301+
if (err.name === 'AbortError')
302+
console.error('The immediate was aborted');
303+
});
304+
305+
ac.abort();
306+
```
307+
308+
```cjs
292309
const { setImmediate: setImmediatePromise } = require('node:timers/promises');
293310

294311
const ac = new AbortController();
@@ -306,7 +323,24 @@ ac.abort();
306323

307324
For `setTimeout()`:
308325

309-
```js
326+
```mjs
327+
import { setTimeout as setTimeoutPromise } from 'node:timers/promises';
328+
329+
const ac = new AbortController();
330+
const signal = ac.signal;
331+
332+
// We do not `await` the promise so `ac.abort()` is called concurrently.
333+
setTimeoutPromise(1000, 'foobar', { signal })
334+
.then(console.log)
335+
.catch((err) => {
336+
if (err.name === 'AbortError')
337+
console.error('The timeout was aborted');
338+
});
339+
340+
ac.abort();
341+
```
342+
343+
```cjs
310344
const { setTimeout: setTimeoutPromise } = require('node:timers/promises');
311345

312346
const ac = new AbortController();

0 commit comments

Comments
 (0)
Please sign in to comment.