Question about valid async syntax in TS/JS
Hello, I recently ran into a problem with some async web calls and Im trying to get to the root cause. I think I fixed it, but I want to learn more about how async works in TS/JS
I basically started with this code:
async function Function1(): Promise<void> {
if(someCondition) {
return Function2() //NOTE: Im returning a promise without awaiting
}
await doWork();
}
async function Function2(): Promise<void> => await work();
I later changed it to this code and my errors seem to be fixed:
async function Function1(): Promise<void> {
if(someCondition) {
await Function2(); //here I await the promise and return after
return;
}
await doWork();
}
async function Function2(): Promise<void> => await work();
I would like to know if the first example is valid JS or if I did correctly identify the issue. I know returning a promise from an async method like this is invalid in C#, but Im not sure about JS.
My follow up question: If this is invalid, why did TS not mark it as an error during compilation?
Posted Aug 12, 2022, 8:23 PM UTC
Nah, the first was valid. Not sure how changing the code how you did changed anything.