#fetch not throwing consistently
1 messages · Page 1 of 1 (latest)
Thanks for creating a thread about this I guess
you mean you are providing the same target id twice and it has different behaviour?
aka: 2. should not happen?
I am not even calling it twice, it just gives me two outputs. And yes, it is not supposed to resolve because the ban doesn't exist.
what do you mean it gives you two outputs?
that entire line will return a Promise instance, where do you get a second one?
Alright, I'll begin new.
I now put the line in a const to log the promise.
In the .catch log it gets me the error I expect (Unknown Ban). return will be called and everythings fine.
However, somehow, the code goes on and the console.log after the promise gives me a Message Object (wut?)
Let me put the second one in a ".then" to be sure
can you show more of the code this is embdedded in?
Alright, I'll do my best to cut it down so you can still see the relevant parts
I hope this'll do
Alright, now the second one returns a snowflake again, I am very confused
First time:
DiscordAPIError: Unknown Ban
Output
959877483526758451
Is the console output for this run
Output = Second time, I changed the name in my code
regarding line 25ff.
which is useful to accomplish new actions even after an action failed in the chain
Give me a mo
What about this https://srcb.in/Mld5Mhqpoc .
When starting it gives me the "First time" log (Unknown Ban) but when I log the fetch afterwards in console.log() which shouldn't be called at all in my opinion it returns a message object.
Am I missing something again?
If you don't mind, please send your answers to my DMs or ping me so I can see it when I come back. I'll be on my phone/tablet but not at my pc.
ah, i see
that's not even a user id
i think what you are seeing here is the return value of the catch
not too sure why that's just an id, though
But what about the return of a message object with the second sourcebin? (After the catch)
In line 35
that is a message object? that's expected
because that's the return value of the #editReply call
Mmh, but why does it continue to execute the code. I return when I call #editReply.
you return a value from the scope of the catch callback
which is what the promise resolves with
which is then also passed to the .then() you chain to the.catch()
-ev new Promise((res, rej) => rej(3)).catch(() => 4)
4```• d.js `14.0.0-dev.1647259751.2297c2b` • Type: `number` • time taken: `0.186941ms`
-ev new Promise((res, rej) => rej(3)).catch(() => 4).then(resolved => resolved)
4```• d.js `14.0.0-dev.1647259751.2297c2b` • Type: `number` • time taken: `0.340529ms`
But usually I don’t have a .then … or does it just virtually add one?
you had here https://sourceb.in/XYHOBdYQ8E l.35
I changed it to be sure that the log comes from that line. Usually I have some kind of #959873041838518302 message
any code you want to execute after the successful completion of the promise has to be within a then callback that is chained before a catch
you return #editReply in both 29 and 32 - which are both possible control branches
that is what tempbanfetch resolves with
and accordingly also what it's value is like 35 ff.
const ban = await bans.fetch(id).then(ban => {
// everything that depends on the ban being fetched successfully
return 1 // what you want "ban" to be in case of success
}).catch(err => {
// everything that needs to happen if the ban fetching was rejected (error handling)
return 0 // what you want "ban" to be in case of the rejection
})
// ban can now be 1 or 0
const ban = await bans.fetch(id).catch(err => {
// everything that needs to happen if the ban fetching was rejected (error handling)
return 0 // what you want "ban" to be in case of the rejection
})
// ban can now be a ban (what the promise resolves with in case of success) or 0
the code will continue in either case with the respective value the promise will resolve with
Soo, what would be the best way to stop the execution (e. g. defining a global car then checking and eventually returning outside then), because I am feeling a bit dumb right now tbh
having all code that requires the successful completion in a single then
or - which i personally prefer - just wrapping the entire scope in a try {} catch {} block
@lilac comet something like
try {
const ban = await fetchBan();
await unban(ban.user.id);
await send("successful unban");
} catch (e) {
logger.error(e);
await send("oop").catch(() => {}) // try to inform that something wrent wrong, ignore if that doesn't work either
}
seems to be much more to the point and easier to wrap one's head around for your use case
if you want more granularity as to what rejected
try {
const ban = await fetchBan();
try {
await unban(ban.user.id);
} catch {
await send("couldn't unban");
return; // returns from the entire surrounding function scope, stops execution
}
await send("successful unban");
} catch (e) {
logger.error(e);
await send("oop").catch(() => {}) // try to inform that something wrent wrong, ignore if that doesn't work either
}
or similar would of course also make sense
- though at that point i'd probably wrap it into functions. at a certain level try-catch-trees become quite the eyesore and semantical error magnet
That explain some things. To be honest I didn’t know that .catch doesn’t end but catch {} does. Thanks for the help :)
well, they both "end" in their own way, right
if you put code outside the catch {} it still keeps executing
the complication is the return values resolving into each other and propagating the value to the assignment
as well as return returning from the function scope
.catch(fn) fn has it's own function scope, returning from it will not return the outer scope ~ "end" execution
So it wouldn’t be possible in a easy way to return the full expression with a catch without putting the rest in a .then it try before?
@lilac cometnot that i know of (since js has no pointers, wouldn't know how to)
i think you'd need to exploit the property of returning something different and then returning the outer scope
const res = await fetchBan().catch(() => 0)
if (!res) { // ban would be truthy, 0 is falsy (just to stick with the 0 from the example before)
return; // return from the outer scope, if falsy return val
}
// do something with res
Yep, that would be my first approach too. Thanks for your help :)
If you don’t mind I’d like to keep this open to note something and then let it archive afterwards
sure, it's set to 1h? if you need more i can extend
If you could set it to one day so I can do it tomorrow, that’d be perfect
24h inactivity set 
Thank you so much 😊
no problem, have fun with your project 
@stable quartz Sorry for the ping but how did you get to your discord username? It reminds me of a movie to be honest
from my mmo character which is derived from log horizon, which i was watching at the time https://log-horizon.fandom.com/wiki/Soujiro_Seta
Soujiro turned into Souji as a nick