Returning values in functions that use asynchronous blocks

Hi, I am wondering if there is a better way of implementing a function with asynchronous block (in particular, Web API) with a return value that depended on the results of the asynchronous block.

I need to call the above function multiple times, and used delay to guarantee correct values are returned, but this way slowed down the my app significantly. Just saw the conditional-return block.

Clipboard02

I was wondering if anyone know how to use this, potentially can improve efficiency of the function?

Thanks

Hi, to work with asynchronous blocks, a recursive function call with this block is used

Yes. I have used the recursive function to make my other wepAPI calls for data. But for this function, the conversion from unixtime to readable date/time function was needed everywhere in my app. They is no there is no way I can make it recursive. Hence trying to find a way to work around.

If you need a function on different screens, then you need to make this function global. You can see an example of a global function on the scrTitle and scrGlobalFunction screens in the project

https://x.thunkable.com/projects/5d70f93f8a62b4079eedaff2/project/properties/designer/

Using delay blocks in asynchronous functions is an unreliable solution that can be a very good and constant source of random errors

Thanks actech. Fantastic demo app. Yes that’s what I need, making the functions global so I can consistently use it across my screens. And I agree that delay blocks with asynchronous functions gave unforeseeable random errors, because I will never know how long it takes for a web call to return the results. I made a work around as shown below:


Basically only to return the value when the Wep API call is guaranteed completed. Not elegant but works all the time.

Separately you know how this block below works? seemed to me as a redundant block?
Clipboard02

This block is used for immediate exit from the function if the condition in it is true. For example, your function should find and return the found value. You can make a condition in the loop and when it is triggered, abort the loop and return the result, or you can immediately abort the function when the condition is met and return the specified value.

1 Like