I'm interested in the case of wrapping an async operation in a timeout for failure to finish. For example, I'm trying to get data with a fetch function, if after 4 seconds it doesn't arrive, have the function return a result indicating the failure.
I tried to figure it out with Promise.race but still couldn't get it to affect the function's return value.
async timeoutedAwait(awaitedFunc,timeoutInMs){
setTimeout(()=>{ /* somehow make the function return {success:false,value:undefined} */},timeoutInMs);
return await awaitedFunc();
}
//....
let result=timeoutedAwait(someFetchOperationFunc,4000);
if(result.success){/* success handling code */}
else{ /* failure handling code */}