Wait for a callback to be executed

I have an asynchronous function with a callback. The callback sets eventsFunctionContext.returnValue from my expresion to what I want, but it takes too long and when I try to get the expressions, it returns undefined because the callback didn’t execute already. Is there a way to make gdevelop wait for that callback to execute?


Foo(function(Response) {
eventsFunctionContext.returnValue = Response.r.toString();

What I’m searching:

A way to tell gdjs/Gdeveleop to wait until the callback is executed to go get the value.


Foo(function(Response) {
    eventsFunctionContext.returnValue = Response.r.toString();
    eventsFunctionContext.valueReady = true;
eventsFunctionContext.valueReady = false;

So the expression will have to wait untill the value ready attribute is set to true before returning it.

I don’t js, but isn’t that the whole point of asynchronous operations, not waiting for completion?

Anyway, when you pick an asynchronous action in GD, there’s always a variable along with it to check for “ok”/“error” status. Can’t you use this status variable as condition? Don’t ask me for js syntax though. :sweat_smile:
If you look at the source code of one of these async actions, you might see how those status variables are handled… I suppose. :grimacing:

Yes it is, but Gdevelop think it’s already complete when it’s not. I search a way to tell gdevelop when it is completed and when it’s not.

Example: Like I posted originally, I call the function and the tell gdevelop that it’s not complete, and when the callback is executed, it tells gdevelop that it is. (The attribute doesn’t exist it’s just an example of what I’m searching).

Found a solution: make an init function that you init with the game. Then create a JavaScript function in the gdevelop function that gets asynchronously the variables and set them to gdjs.ExtensionName.variableName (create it before defining the function with gdjs.ExtensionName = {}:wink: then just use seTimeout to make the function call itself back after a few milliseconds (example: 100ms) and call that function. Then just get in the expression gdjs.ExtensionName.variableName and set eventsFunctionContext.returnValue to it.

1 Like

It’s a late reply maybe but i started newly to Gdevelop.
Canyou send the example of your solution. Or else can someone explain how can we wait for a javascript block to return a value from an API.

I managed to get response from API. But it takes a long to to get a response from server.
So i have to wait and be sure the response recieved.

Found another solution described it here:

1 Like