#Why is await on the HTTPRequest not stopping the code from executing?

15 messages · Page 1 of 1 (latest)

rose mortar
#

Please ignore most of the ugliness in this code, what I'm trying to figure out is why the PlanetDeath.emit() function is running before the HTTPRequest is able to fire. When the signal is emitted, it restarts the tree resetting the state. My current guess is that it's resetting the tree so fast that the HTTPRequest is not able to actually get sent completely. I've tried adding a bunch of awaits to my code to try and make it wait for the response but can't seem to figure out what I'm doing wrong.

If I remove this signal.emit() and let it take a couple of frames, it does end up sending the signal and I can see it being received on the server side. Is there something I'm missing here with awaits?

grim star
#

the update_leaderboard function does not await for anything else inside of it

#

awaiting for a function that doesn't await for something else has no effect

#

it just runs normally (immediately, without waiting for anything else)

#

you probably forgot to await for the HTTPRequest's request_completed signal

#

btw when using await with a signal that has multiple arguments, the await will receive them in an array

#

so if you do var result = http_request.request_completed, result will be an array where result[0] is the error code int, result[1] is the status code, result[2] is the headers, and result[3] is the response body

#

and in your second screenshot, request_leaderboard is using await twice in line 10 but both of them have no effect

#

connect() returns immediately, it's just adding a function to the list of connections of that signal, so awaiting it does nothing (as connect() does not return the signal itself)

#

and await self._on_http_request_completed is awaiting on a callable value (which is just getting a reference to a function of this script, not running it), so there's nothing to await here either

#

awaits like that resolve immediately, as if you never wrote the await

#

you can only await signals (fully, not results of other functions on them like connect()), like
await http_request.request_completed; and calls of script functions that use await inside of them, like await my_func(), where my_func either awaits for another script function call or a signal

#

anything else will ignore the await

#

await can make code order of execution confusing (when they do something at least), so usually it's preferred to use connect() with functions instead when possible, and you can also create your own custom signals and emit them to send results back from the functions connected to the built-in signals