Documentation Index
Fetch the complete documentation index at: https://mintlify.com/visible/cruel/llms.txt
Use this file to discover all available pages before exploring further.
Function Signature
function withTimeout<T extends AnyFn>(
fn: T,
options: TimeoutOptions
): T
Wraps a function with timeout protection. Throws CruelTimeoutError if the function doesn’t complete within the specified time.
Options
Timeout duration in milliseconds. Must be at least 1.
Callback invoked when the timeout is triggered.
Return Type
Returns the wrapped function with timeout protection.
Types
interface TimeoutOptions {
ms: number
onTimeout?: () => void
}
Example
import { withTimeout, CruelTimeoutError } from 'cruel'
const timedFetch = withTimeout(
async (url: string) => {
const response = await fetch(url)
return response.json()
},
{
ms: 5000,
onTimeout: () => {
console.error('Request timed out after 5s')
}
}
)
try {
const data = await timedFetch('https://api.example.com/slow-endpoint')
} catch (error) {
if (error instanceof CruelTimeoutError) {
console.log('Request exceeded timeout')
}
}