@consumidor-positivo/ts-utils - v1.13.0
    Preparing search index...

    Function delay

    • Creates a promise that resolves after the specified duration.

      This utility function is useful for introducing delays in async operations, implementing timeouts, or controlling the flow of asynchronous code.

      Type Parameters

      • T = void

      Parameters

      • ms: number

        The delay duration in milliseconds

      • Optionalvalue: T

        Optional value to resolve the promise with

      Returns Promise<undefined | T>

      A promise that resolves after the specified delay with the provided value

      import { delay } from '@consumidor-positivo/ts-utils';

      // Basic usage - wait for 1 second
      await delay(1000);
      console.log('This logs after 1 second');

      // Chain with other async operations
      async function fetchWithDelay() {
      await delay(500); // wait 500ms before fetching
      return fetch('https://api.example.com/data');
      }

      // Use with a value to pass through the promise chain
      const result = await delay(1000, 'resolved value');
      console.log(result); // 'resolved value' after 1 second