# Returning values in functions that use asynchronous blocks

**URL:** <https://community.thunkable.com/t/returning-values-in-functions-that-use-asynchronous-blocks/599348>\
**Category:** Questions about Thunkable X\
**Tags:** asynchronous\
**Created:** [June 7, 2020, 9:04am UTC](https://community.thunkable.com/t/returning-values-in-functions-that-use-asynchronous-blocks/599348 "2020-06-07T09:04:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gobassky](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/gobassky/32/77019_2.png) [@gobassky](https://community.thunkable.com/u/gobassky)\
**Post date:** [June 7, 2020, 9:04am UTC](https://community.thunkable.com/t/returning-values-in-functions-that-use-asynchronous-blocks/599348/1 "2020-06-07T09:04:14Z")

</div>

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.

 ![Clipboard01](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/7/2/72fe685f129f7fc749d1f0454b871b1a5b36955a.jpeg)

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](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/8/1/81f0115e2305fac4dfbccfda612b7dd834529838.jpeg)

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

Thanks

---

<div class="post-metadata">

**Author:** ![actech](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@actech](https://community.thunkable.com/u/actech)\
**Post date:** [June 7, 2020, 10:04am UTC](https://community.thunkable.com/t/returning-values-in-functions-that-use-asynchronous-blocks/599348/2 "2020-06-07T10:04:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gobassky](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/gobassky/32/77019_2.png) [@gobassky](https://community.thunkable.com/u/gobassky)\
**Post date:** [June 8, 2020, 8:33am UTC](https://community.thunkable.com/t/returning-values-in-functions-that-use-asynchronous-blocks/599348/3 "2020-06-08T08:33:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![actech](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@actech](https://community.thunkable.com/u/actech)\
**Post date:** [June 8, 2020, 10:47am UTC](https://community.thunkable.com/t/returning-values-in-functions-that-use-asynchronous-blocks/599348/4 "2020-06-08T10:47:08Z")

</div>

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/](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

---

<div class="post-metadata">

**Author:** ![gobassky](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/gobassky/32/77019_2.png) [@gobassky](https://community.thunkable.com/u/gobassky)\
**Post date:** [June 8, 2020, 1:41pm UTC](https://community.thunkable.com/t/returning-values-in-functions-that-use-asynchronous-blocks/599348/5 "2020-06-08T13:41:57Z")

</div>

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:

 ![Clipboard03](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/2/d/2dff22f309c289eb6a972261c1e0b23507003f7a.jpeg)  
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](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/8/1/81f0115e2305fac4dfbccfda612b7dd834529838.jpeg)

---

<div class="post-metadata">

**Author:** ![actech](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@actech](https://community.thunkable.com/u/actech)\
**Post date:** [June 8, 2020, 2:29pm UTC](https://community.thunkable.com/t/returning-values-in-functions-that-use-asynchronous-blocks/599348/6 "2020-06-08T14:29:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ioannis](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/ioannis/32/146956_2.png) [@ioannis](https://community.thunkable.com/u/ioannis)\
**Post date:** [November 8, 2024, 11:27am UTC](https://community.thunkable.com/t/returning-values-in-functions-that-use-asynchronous-blocks/599348/7 "2024-11-08T11:27:42Z")

</div>


