I am trying to write a NodeJS script that will return gracefully if it takes too long.
So far I have:
async function willTimeout(){
try{
new Promise((resolve, reject) => {
setTimeout(reject, 1, "timeout");
}).catch(error => {
throw error;
});
await variableLengthFunction()
}catch{
//Handle error gracefully here
}
}
This doesn't work because the .catch()
in the promise doesn't return gracefully as it is an unhandled promise rejection. Is there any way to return an error from a promise that is caught by the encompassing try block?