es-next-tools
Version:
A comprehensive utility library for JavaScript and TypeScript that provides a wide range of functions for common programming tasks, including mathematical operations, date manipulations, array and object handling, string utilities, and more.
14 lines (13 loc) • 564 B
JavaScript
/**
* Delays the resolution of a promise or value by a specified number of milliseconds.
* @param {T | Promise<T>} value - The value or promise to delay.
* @param {number} ms - The number of milliseconds to delay.
* @returns {Promise<T>} A promise that resolves with the value after the specified delay.
* @template T
* @example
* const delayedValue = await delay('Hello', 1000);
* console.log(delayedValue); // Logs 'Hello' after 1 second
*/
export function delay(value, ms) {
return new Promise((resolve) => setTimeout(() => resolve(value), ms));
}