Sleeps for the given number of milliseconds.
给定毫秒数睡眠。
The number of milliseconds to sleep. 睡眠的毫秒数
The options for the sleep. 睡眠的配置项
The signal to abort the sleep. 睡眠的中止信号
A promise that resolves after the specified delay. 在指定延迟后解析的Promise
If the sleep is aborted via the signal. 如果通过信号中止睡眠
console.log('Start');await sleep(1000); // Delays execution for 1 secondconsole.log('End'); Copy
console.log('Start');await sleep(1000); // Delays execution for 1 secondconsole.log('End');
const controller = new AbortController();const { signal } = controller;setTimeout(() => controller.abort(), 50); // Will cancel the delay after 50mstry { await sleep(100, { signal });} catch (error) { console.error(error); // Will log 'AbortError'} Copy
const controller = new AbortController();const { signal } = controller;setTimeout(() => controller.abort(), 50); // Will cancel the delay after 50mstry { await sleep(100, { signal });} catch (error) { console.error(error); // Will log 'AbortError'}
Sleeps for the given number of milliseconds.
给定毫秒数睡眠。