Async in the right place

July 12, 2023

This

1async handleCreateOrUpdateEvent(event: Event): Promise<CreationOrUpdateResult> {
2
3 try{
4 return this.handleEvent(event);
5 }catch(e){
6 console.log(e);
7 return {
8 result: "failure",
9 errorData: {
10 channelMessage: e.message
11 }
12 }
13 }
14
15 }

and this

1async handleCreateOrUpdateEvent(event: Event): Promise<CreationOrUpdateResult> {
2
3 try{
4 return await this.handleEvent(event);
5 }catch(e){
6 console.log(e);
7 return {
8 result: "failure",
9 errorData: {
10 channelMessage: e.message
11 }
12 }
13 }
14
15 }

Are not behaving the same way. Have you spotted the difference?

I admit I encountered this a couple of times and this is a great sources of errors difficult to handle.

A try catch block is allowed on an async method only if the await are explicit. So both return x; and return await x; are equivalent from the point of view of the method signature but in the first case it's not possible to implement a try catch block.

Of course, a potential solution would be to use catch()