handle-cli-error
Version:
💣 Error handler for CLI applications 💥
39 lines (25 loc) • 613 B
JavaScript
export const validateTimeout=(timeout,optName)=>{
if(
(!Number.isInteger(timeout)||timeout<=0)&&
!isSpecialTimeout(timeout))
{
throw new Error(
`"${optName}" must be 0, a positive integer or Infinity: ${timeout}`
)
}
};
const isSpecialTimeout=(timeout)=>
timeout===INFINITE_TIMEOUT||timeout===NO_TIMEOUT;
export const waitForTimeout=(timeout,callback)=>{
if(timeout===NO_TIMEOUT){
callback();
return
}
if(timeout===INFINITE_TIMEOUT){
return
}
setTimeout(callback,timeout).unref()
};
export const NO_TIMEOUT=0;
export const INFINITE_TIMEOUT=Number.POSITIVE_INFINITY;
export const DEFAULT_TIMEOUT=5e3;