timersPromises.setTimeout([delay[, value[, options]]])
delay
<number> 在履行 promise 之前等待的毫秒数。 默认值:1
。value
<any> 履行 promise 使用的值。options
<Object>ref
<boolean> 设置为false
以指示调度的Timeout
不应要求 Node.js 事件循环保持活动状态。 默认值:true
。signal
<AbortSignal> 可选的AbortSignal
,可用于取消调度的Timeout
。
import {
setTimeout,
} from 'timers/promises';
const res = await setTimeout(100, 'result');
console.log(res); // 打印 'result'
const {
setTimeout,
} = require('node:timers/promises');
setTimeout(100, 'result').then((res) => {
console.log(res); // 打印 'result'
});
delay
<number> The number of milliseconds to wait before fulfilling the promise. Default:1
.value
<any> A value with which the promise is fulfilled.options
<Object>ref
<boolean> Set tofalse
to indicate that the scheduledTimeout
should not require the Node.js event loop to remain active. Default:true
.signal
<AbortSignal> An optionalAbortSignal
that can be used to cancel the scheduledTimeout
.
import {
setTimeout,
} from 'timers/promises';
const res = await setTimeout(100, 'result');
console.log(res); // Prints 'result'
const {
setTimeout,
} = require('node:timers/promises');
setTimeout(100, 'result').then((res) => {
console.log(res); // Prints 'result'
});