R
Reactiflux

help-js

⛄Snowberb⛄ – 10-12 May 18

S⛄Snowberb⛄5/18/2023
Why doesnt the try catch finally of this code respect the awaits, but if it is inside the (async () => {})() it does? I dont understand the syntax here
try {
(async () => {
if (!...) {
const ... = await ...;
...
return;
}
await ...
})();
} catch (e) {} finally {}
try {
(async () => {
if (!...) {
const ... = await ...;
...
return;
}
await ...
})();
} catch (e) {} finally {}
The above doesnt work, but this does:
(async () => {
try {
if (!...) {
const ... = await ...;
...
return;
}
await ...
} catch (e) {} finally {}
})();
(async () => {
try {
if (!...) {
const ... = await ...;
...
return;
}
await ...
} catch (e) {} finally {}
})();
EEva5/18/2023
Because you don't catch unawaited async functions
try {
await (async () => {
if (!...) {
const ... = await ...;
...
return;
}
await ...
})();
} catch (e) {} finally {}
try {
await (async () => {
if (!...) {
const ... = await ...;
...
return;
}
await ...
})();
} catch (e) {} finally {}
would work if IIFE throws something, you'd need another try catch inside of function to catch calls inside of it, obviously
UUUnknown User5/18/2023
Message Not Public
Sign In & Join Server To View
S⛄Snowberb⛄5/18/2023
didnt understand
EEva5/18/2023
first piece of code you sent can be simplified to
try {
asyncFunction();
catch (e) {} finally {}
try {
asyncFunction();
catch (e) {} finally {}
you don't await async function so if it throws you don't catch anything
S⛄Snowberb⛄5/18/2023
oh
EEva5/18/2023
and it won't recursively catch anything within that function as well
UUUnknown User5/19/2023
Message Not Public
Sign In & Join Server To View

Looking for more? Join the community!