Consider the following two code snippets (assumed to be inside an async function that returns void / we don't care about the return value):
Long version (without await):
if (condition) {
conditionalCodeBlock.then(unconditionalCodeBlock);
} else {
unconditionalCodeBlock;
}
Short version (with await):
if (condition) {
await conditionalCodeBlock;
}
unconditionalCodeBlock;
Both code snippets do the same thing. The short version doesn't have to repeat unconditionalCodeBlock in the two cases, but it somewhat hides the fact that unconditionalCodeBlock is being invoked in different ways: asynchronously if condition is true, and asynchronously if false.
My colleague thinks that sweeping this complexity under the rug can be misleading and make it harder to reason about the code, so he prefers the longer version.
Is this concern valid: Does this matter in practice (and in what situations), and if so, there is a way to address this concern this without having to repeat unconditionalCodeBlock?
(Some more background on how this question arose, and illustration/verification of how async works in this case, are in the edit history (second revision) of this question.)
Neither syntax is better or less misleading.
The actual problem is that by executing the block sometimes-synchronously sometimes-asynchronously there is potential to be mislead at all. Don't make it hard to reason about in the first place! In the majority of cases, it actually doesn't matter for any reasoning whether the code executes synchronously or not. If that is the case, you can choose either syntax, and you'd prefer the shorter and simpler one, in particular without the repetition.
However if you have one of the rare cases where it matters for reasoning, you should strife towards making reasoning easier by consistently executing the code always asynchronously and thereby avoiding zalgo. In your example, that might be
(the comment is important so that someone doesn't optimise the
awaitaway by cleaning up this slightly weird code, and breaks the assumptions you've made or even the guarantees you've documented for your API)If that isn't possible either and you absolutely need to sometimes execute the code synchronously for whatever reason, formatting still won't help you. You have to add a comment where you clearly state this reason, and where you lay down the reasoning about (a)sync execution that you've already done yourself.