For the past six months, I have been utilizing async/await and have truly enjoyed the convenience it provides. Typically, I adhere to the traditional usage like so:
try {
await doSomethingAsync()
}
catch (e) {}
Lately, I've delved into experimenting with not immediately awaiting in order to execute additional code before waiting for the async data, such as:
let p = doSometiongAsync()
... perform more tasks
await p;
or:
let p1 = doJob1();
let p2 = doJob2();
... engage in more synchronous activities ...
await p1;
await p2;
The dilemma arises when considering where to properly place the try/catch blocks to ensure that errors, whether synchronous or asynchronous, within the calls are caught effectively. Should the try/catch block encompass the initial function call:
try {
let p = doSomethingAsync()
} catch(errors) {}
... undertake actions ...
await p
Should the try block be positioned around the await statement... or possibly two separate try blocks, one for each?
try {
let p = doSomethingAsync()
} catch(errors) {}
... carry out tasks
try {
await p;
} catch (evenmoreerrors) {}
Thank you!