How to?! domSide.js: async functions

0 favourites
  • 3 posts
  • Hi there,

    I have two questions:

    1.) How can I handle asynchronous functions in domeSide.js? At the moment it is very complicated:

    Example:

    Is there an easier way? I couldn't use await.

    this.AddRuntimeMessageHandlers([
    	["get-value", () => this._GetValue()]
    ]);
    
    _GetValue() {
    	asyncFunction().then(value => {
    		this.PostToRuntime("set-value", value);
    	});
    }
    

    I need two MessageHandlers. Is there an easier way? I couldn't use await.

    What obviously doesn't work:

    _GetValue() {
    	const value = await asyncFunction();
    	return value;
    });
    
    _GetValue() {
    	asyncFunction().then(value => {
    		return value;
    	});
    }
    

    2.)

    It looks like you can use _postToDOMAsync in conditions. Is it correct that this doesn't work with expressions?

    Thank you very much

  • To use "await", you simply have to precede the function with "async". Like this:

    async _GetValue() {
    	const value = await asyncFunction();
    	return value;
    });
    

    The second question is still open.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • It looks like you can use _postToDOMAsync in conditions. Is it correct that this doesn't work with expressions?

    You can call the function, but I would say it's not useful, because conditions and expressions must return a result synchronously, so you can't wait on anything in them. If you want to do async work it should be in an action, since that is the only thing that the engine allows to finish working later on.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)