Unwrapping `Promise<x>` where `void` expected?
Hi.
It's my first time moving to TypeScript from Rust (
serenity+poise
->discord.js
) and I am trying to handle a GuildUpdate
event where ownership has been transferred to another.
It seems that doing this gives me a Promise<string>
which I can't seem to unwrap.
When attempting to do so, it asks me to async
the part after the event, like so:
...but then, it tells me that it expects a void
return? In serenity
, I just made a function async
and handled everything else by calling .await
or .await?
at the end, depending on whether or not something was a Result<x, y>
or not.
How exactly am I able to handle the case here?31 Replies
- What's your exact discord.js
npm list discord.js
and node node -v
version?
- Not a discord.js issue? Check out #other-js-ts.
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!
- ✅
Marked as resolved by OP1) I use Bun, so
bun -version
is 1.1.8
u resolve the promise by using
await
, u dont need the then
if you're using await
as i said, it starts complaining that it cannot have
async
on a void
type, because to have an await
call, it requires an async
declaration.Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
so...it's a double
await
that i have to double-handle?Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
well, i have the
await
appended, so I get this issueu can just access the username like that
(await client.users.fetch(oldGuild.ownerId)).username
I do what it asks, and then we get here
so exactly what does it want?
Unknown User•8mo ago
Message Not Public
Sign In & Join Server To View
aaagh
this feels extremely weird coming from Rust
this sounds more like an issue with how bun handles typescript if you're executing it directly
void functions should be able to return anything
though that error definitely just looks like a linting error and not a typescript error
so it's not a typescript issue?
so this function is correct?
can i... make the function return a Promise so that the warning goes away?
async functions always return promises
this is also the reason you were receiving the linting error in the first place
hmm
is that a proper solution though?
i'm unclear on whether or not the code itself would be idiomatically correct to a strict TypeScript standard
i applied OP's final solution and the warning went away:
looks ugly, but if it works, it's not that stupid
wouldn't the warning go away if u didnt use then?
i have to use
then
to get the username from user
from userId
it's almost like the equivalent of using a match
statement to unwrap bits and piecesu dont need to use then if you're using await
const oldOwnerUsername = (await client.users.fetch(oldGuild.ownerId)).username;
wait, really?
interesting
await resolves the promise so
(await client.users.fetch(oldGuild.ownerId)
is of type Userwhat you're awaiting is the .then() since that returns a promise
that was a very rustic way of doing it lole
it's schtill odd they haven't provided a helper function for returning an
owner
as User
not sure if the warn would go away if you didn't use then though, it seems like the issue is the fact that the callback is async
it's not back thanks to the awful workaround from the linked issue solution
:method: Guild#fetchOwner
@14.15.2
Fetches the owner of the guild. If the member object isn't needed, use ownerId instead.oh?
seems to not warn
do consider that that returns a guildmember, not a user, but it still works
yep
seems more idiomatic since we are directly calling from the guild cache, if any
users.fetch also checks cache iirc
at least i don't have to do this absolute disgust in Disc.js
i'm so thanksful for built-in helper functions
just get a
guild
arg directly and call from there
now i'll have to test out that event to see if it's correct
but for now, issue ist solved. thank you!