mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
doc: add esm examples to node:timers
PR-URL: https://github.com/nodejs/node/pull/55857 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Jason Zhang <xzha4350@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
parent
cb7d855f88
commit
d894772157
@ -292,7 +292,24 @@ returned Promises will be rejected with an `'AbortError'`.
|
|||||||
|
|
||||||
For `setImmediate()`:
|
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 { setImmediate: setImmediatePromise } = require('node:timers/promises');
|
||||||
|
|
||||||
const ac = new AbortController();
|
const ac = new AbortController();
|
||||||
@ -310,7 +327,24 @@ ac.abort();
|
|||||||
|
|
||||||
For `setTimeout()`:
|
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 { setTimeout: setTimeoutPromise } = require('node:timers/promises');
|
||||||
|
|
||||||
const ac = new AbortController();
|
const ac = new AbortController();
|
||||||
|
Loading…
Reference in New Issue
Block a user