A common pattern is to call an async function and return its result:
const helloWorld = async () => {
return await asyncHello("World");
}
Why the await
? It would work exactly the same without it, so it’s just taking up space.
Yes, but: let’s add a try..catch
block around it:
try {
return await asyncHello("World");
}catch(e) {
return "Whops";
}
It works as expected: if the called function throws an error (rejects its Promise) then the catch
block will run.
But now there is a difference:
try {
return asyncHello("World");
}catch(e) {
return "Whops";
}
Why didn’t the try..catc