Skip to content

Commit

Permalink
doc: add esm examples to node:timers
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
mfdebian authored and aduh95 committed Dec 10, 2024
1 parent 12ebb56 commit 41e5cab
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions doc/api/timers.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,24 @@ 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;

// We do not `await` the promise so `ac.abort()` is called concurrently.
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();
Expand All @@ -310,7 +327,24 @@ ac.abort();

For `setTimeout()`:

```js
```mjs
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) => {
if (err.name === 'AbortError')
console.error('The timeout was aborted');
});

ac.abort();
```

```cjs
const { setTimeout: setTimeoutPromise } = require('node:timers/promises');

const ac = new AbortController();
Expand Down

0 comments on commit 41e5cab

Please sign in to comment.