logo@krutoo/utils

wait

Also known as sleep and delay

Returns Promise that resolves after given timeout.

import { wait } from '@krutoo/utils';

// with async/await
console.log('Start...');
await wait(500);
console.log('Timeout passed');

// without async/await
console.log('Start...');
wait().then(() => {
  console.log('Timeout passed');
});

Aborting

You can pass abort signal from AbortController to reject promise:

import { wait } from '@krutoo/utils';

const controller = new AbortController();

wait(1000, { signal: controller.signal })
  .then(() => {
    // ...
  })
  .catch(reason => {
    console.log(reason); // "Fake reason"
  });

controller.abort('Fake reason');