模拟延迟

语法

  1. import { imitateDelay } from 'warbler-js';
  2. imitateDelay(timeout);

参数

  • timeout (Number) : 延迟的时间,单位毫秒(ms)。

返回值

PromisePromise

源码

  1. const imitateDelay = (timeout) =>
  2. new Promise((resolve) => {
  3. const timeoutHandle = setTimeout(() => {
  4. clearTimeout(timeoutHandle);
  5. resolve();
  6. }, timeout);
  7. });

例子

  1. import { imitateDelay } from 'warbler-js';
  2. async function test() {
  3. console.log('The first log');
  4. await imitateDelay(1000);
  5. console.log('The second log with 1000 ms delay'); // => 1000 毫秒后输出 The second log with 1000 ms delay
  6. }
  7. test();