Another pattern to avoid the above is to remember that async functions return promises and that .catch() also returns a promise. So your above logic can be written as:
const result = await funcThatReturnSomeType().catch(doSomethingWithErr);
if (result) {
doSomething(result);
} const result = await funcThatReturnSomeType().catch(convertError); // result: SomeType | Error
if (isError(result)) return result;
// now result: SomeType
EDIT: the ? operator in question - https://doc.rust-lang.org/edition-guide/rust-2018/error-hand... function doSomethingWithErr(err: any): never {
throw new Error("Oops");
}
let result: SomeType;
try {
result = await funcThatReturnSomeType();
} catch (err) {
doSomethingWithErr(err);
}
// because doSomethingWithErr has return type "never", result will be definitely assigned.
doSomething(result);
..or just return in the catch block. (() => {
try {
// finally will return prior to this console.log
console.log('this try was executed and the return ignored')
return 'try block'
} catch (e) {
return 'error block'
} finally {
return 'finally block'
}
})()Basically, `finally` gives you a guarantee that it will actually run once the `try` block is exited. Likewise, `return` effectively assigns the return value and exits. But it doesn't (cannot and should not) breach the contract of try-finally, since the purpose of try-finally is to ensure resources are managed correctly, so it either exits to the caller or it exits to the finally block, depending on which one is most recent.
In your case, a return value is assigned and the `try` block is exited using `return`. We then have to continue into the `finally` block, since that is the core meaning of `finally` - we run it after we leave `try`. And then with `return`, we reassign the return value and finally leave the whole function. At this point, the return value is the second one that was assigned to it.
Maybe thinking of it like this is helpful, although I somewhat hope it isn't. You can see that "return" is reassigned before we have a chance to read it. I've simplified by removing any consideration of errors, but I console.logged the final output.
//this is the function call at the end of your IIFP
next_code.push(AfterMe)
goto Anonymous
// this is the function definition
Anonymous:
// this is the try-finally idiom
next_code.push(FinallyBlock);
//this is the try
console.log("this try was executed");
//these two lines are the first return
var return = 'try block';
goto next_code.pop();
//this is the finally
FinallyBlock:
var return = 'finally block'
goto next_code.pop();
// this code gets executed from the FinallyBlock's goto and is as if you have a console.log(..) around your whole definition.
AfterMe:
console.log(result)Unless one goes the PowerShell way and just forbids returning from finally.
And now you wrap the function call to doSomething() in the try/catch too. Often (usually?) the try/catch specifically is for the asynchronous function. Usually that's because the async. stuff might fail due to expectable (even if undesirable) runtime conditions (e.g. "file not found"), while synchronous code should work for the most part (external condition errors vs. coding errors - and your catch is about the former, because, for example, you might want coding errors to just crash the app and be caught during testing).
Sure, you can claim that you check for specific errors that could only happen in that function, so that any errors occurring in doSomething() don't matter/don't change the outcome, or that doSomething never throws because you are sure of the code (the async. function may throw based on runtime conditions, but you may have development-time control over any issues in doSomethign() - but if you start going down that path, having to rely on the developer doing there job perfectly for each such construct, later maintainability and readability goes down the drain. You would have to make sure such a claim is valid when you later come across the construct. That is why you really don't want anything inside the try/catch from which you don't want to see any errors in the catch block. So my policy is to never ever do that even if in the given context it would work - it places additional work on whoever is going to read that section later (or they are ignorant of the problem and won't see this potential problem, which is not any better).
My solutions have involved casts (and comments explaining the assumptions involved) instead of the ‘if’ statement more times than I’d like to have done, but it ends up with the same result with the added (however small) compute with the conditional since it can be safe to assume the value is not undefined. It’s not perfect, but it at least omits unnecessary runtime code.
At any rate, I’d appreciate the same thing you would in these cases.
const [err, result] = await to(funcThatReturnSomeType());
if(err) doSomethingWithErr(err);
doSomething(result);
const result = await (() => {
try {
return funcThatReturnSomeType();
} catch (err) {
doSomethingWithErr(err);
}
})(); const result = await (async () => {
try {
return await funcThatReturnSomeType();
} catch (err) {
doSomethingWithErr(err);
}
})(); async function test() {
const promise = Math.random() < 0.5 ?
Promise.resolve('wow') :
Promise.reject('oops');
try {
await promise;
} catch (e) {
console.log('caught');
return;
}
const result = await promise;
console.log('result', result);
}
test(); try {
doSomething(await funcThatReturnSomeType());
} catch (err) {
doSomethingWithErr(err);
}
One of my remaining gripes with Javascript/Typescript is the try/catch mess around await. It makes assignment of const a pain for async calls that may reject.
e.g.
I really want some kind of structures that allow me to make `result` constant. In some cases I've rolled my own Maybe/Either wrapper and then move the try/await/catch into a function but that is still a pain.This is such a common pattern in my code ... I wish there was a more elegant way to deal with it.