Create a delayed execution function that executes a specified function after a specified time.
| Argument |
Description |
Type |
Function |
Returns a function that accepts delay time parameter |
(duration: number) => Promise<U> |
| Parameter |
Description |
Type |
Default |
handler |
Function to execute |
Function |
Required |
...params |
Parameters to pass to function |
T[] |
Required |
import { durationHandler } from 'ranuts';
const delayedFn = durationHandler((name) => {
console.log('Hello', name);
return 'done';
}, 'World');
// Execute after 1 second
const result = await delayedFn(1000);
console.log(result); // 'done'
import { durationHandler } from 'ranuts';
const delayedRequest = durationHandler(async (url) => {
const response = await fetch(url);
return response.json();
}, 'https://api.example.com/data');
// Execute request after 2 seconds
const data = await delayedRequest(2000);
console.log(data);
import { durationHandler, imageRequest } from 'ranuts';
// Create delayed image request function
const delayedImageRequest = durationHandler(imageRequest, 'https://example.com/test.jpg');
// Execute after 3 seconds
const latency = await delayedImageRequest(3000);
console.log('Latency:', latency, 'ms');
- Curried function: Returns a function that accepts delay time parameter, supports functional programming.
- Async support: Supports async functions, will wait for function execution to complete.
- Error handling: If function execution fails, Promise will reject.
- Use case: Commonly used for delayed execution, scheduled tasks, network testing, etc.