# Asynchronous Blocks within Loops

**URL:** <https://community.thunkable.com/t/asynchronous-blocks-within-loops/33524>\
**Category:** Questions about Thunkable X\
**Tags:** loops, asynchronous, realtimedb, hints\
**Created:** [June 28, 2018, 6:47pm UTC](https://community.thunkable.com/t/asynchronous-blocks-within-loops/33524 "2018-06-28T18:47:58Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/mark/32/18133_2.png) [@Mark](https://community.thunkable.com/u/Mark)\
**Post date:** [June 28, 2018, 6:47pm UTC](https://community.thunkable.com/t/asynchronous-blocks-within-loops/33524/1 "2018-06-28T18:47:58Z")

</div>

We recently had a bug reported by a user who was seeing strange behaviors within the “then do” part of a `RealtimeDB.Save` block which was within a loop block. The strange part was that even though each execution of the `RealtimeDB.Save` block was called with the current value of the loop variable (or something based on the current value) the blocks within the “then do” part always just saw the **last** value of the loop variable!

This is a well known issue when using asynchronous function blocks (like `RealtimeDB.Save`) within a loop. Asynchronous functions are ones which run independently of the other blocks that they are contained within. They exist because you usually do not want functions like `RealtimeDB.Save`, which might take a long time to execute, to block the execution of other code. `RealtimeDB.Save` can take a long time to execute because it sends and receives data over the web.

In the long run, we need to make it easier to deal with scenarios like this. In the short run, here’s a workaround. The basic idea is to create your own recursive looping function block. Recursive functions are ones that call themselves within their definition. I have created a project which shows a (hopefully) simple example of this, looping over `RealtimeDB.Save`. That project can be copied by following [this link](https://x.thunkable.com/projects/5b35186038d4a31a6690d7ef/Screen1/designer).

The app has two buttons which when pressed will loop from 1 to 10, storing the loop variable values into a `RealtimeDB` and then displaying the loop variable values after each `Save`. The first button uses a normal loop and show the behavior that you are complaining about. The second button uses the approach that I am suggesting as a workaround and shows all the loop variable values.

There’s also a third button that you can press to get the value stored at a particular key (which you can enter via a `Text Input`) in the `RealtimeDB`. That functionality is just provided for convenience and testing.

The key part of the project is seen in these blocks:  
 ![screen shot 2018-06-28 at 11 16 57 am](https://user-images.githubusercontent.com/1743690/42052895-e8693798-7ac4-11e8-9c47-dc0151e0dc5b.png)

You’ll need to adapt those blocks for your own use.

I hope this helps.

Happy Thunking!

-Mark

---

<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 28, 2018, 8:21pm UTC](https://community.thunkable.com/t/asynchronous-blocks-within-loops/33524/2 "2018-06-28T20:21:18Z")

</div>

Hello Mark,

In your block I see recursion, but in my simple project recursion results in the appearance of a white screen on iOS and Android.

[https://x.thunkable.com/projects/5b353cf538d4a3763090d893/project/properties/designer/](https://x.thunkable.com/projects/5b353cf538d4a3763090d893/project/properties/designer/)

![recursion](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/7/a/7a5761d7e065b006d3eff6fc6ac1d2f901749492.png)

Is recursion working in Thunkable X?

Regards  
Alex

---

<div class="post-metadata">

**Author:** ![Mark](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/mark/32/18133_2.png) [@Mark](https://community.thunkable.com/u/Mark)\
**Post date:** [June 28, 2018, 10:09pm UTC](https://community.thunkable.com/t/asynchronous-blocks-within-loops/33524/3 "2018-06-28T22:09:51Z")

</div>

@actech

Александр,

When doing recursion you need some condition which eventually will end the recursion. Otherwise you end up with an infinite loop or a stack overflow (which is probably what is happening in your case). You’ll notice in my example that I only make the recursive call to `AsynchLoop` when `currentVal <= endVal`. That provides a way for the recursion to end when (i.e. when `currentVal > endVal`).

-Mark

---

<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 29, 2018, 4:56am UTC](https://community.thunkable.com/t/asynchronous-blocks-within-loops/33524/4 "2018-06-29T04:56:07Z")

</div>

Oh, thanks Mark!

When someone takes too long on the road, he sees the mountains well in the distance and does not notice the stones under his feet, as in my case!

Alex

---

<div class="post-metadata">

**Author:** ![Vaibhav\_Patil](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/vaibhav_patil/32/13687_2.png) [@Vaibhav\_Patil](https://community.thunkable.com/u/Vaibhav_Patil)\
**Post date:** [June 29, 2018, 6:30am UTC](https://community.thunkable.com/t/asynchronous-blocks-within-loops/33524/5 "2018-06-29T06:30:37Z")

</div>

> [@actech](#):
>
> When someone takes too long on the road, he sees the mountains well in the distance and does not notice the stones under his feet

👌👌😃

---

<div class="post-metadata">

**Author:** ![Rob\_Schoemaker](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/rob_schoemaker/32/21023_2.png) [@Rob\_Schoemaker](https://community.thunkable.com/u/Rob_Schoemaker)\
**Post date:** [June 29, 2018, 12:22pm UTC](https://community.thunkable.com/t/asynchronous-blocks-within-loops/33524/6 "2018-06-29T12:22:05Z")

</div>

@Mark Thanks for your explanation! Maybe it is an idea to add to the documentation of each block if the block is blocking/non-blocking (synchronous/asynchronous)?

(I guess that it can be seen from the structure of a block (if it contains a “when … done” part it is asynchrounous?) but I am not sure about that)

Thanks!  
Rob

---

<div class="post-metadata">

**Author:** ![Mark](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/mark/32/18133_2.png) [@Mark](https://community.thunkable.com/u/Mark)\
**Post date:** [June 29, 2018, 5:07pm UTC](https://community.thunkable.com/t/asynchronous-blocks-within-loops/33524/7 "2018-06-29T17:07:08Z")

</div>

@Rob_Schoemaker,

Great idea! Thanks.

-Mark

---

<div class="post-metadata">

**Author:** ![sidpalas](https://avatars.discourse-cdn.com/v4/letter/s/dbc845/32.png) [@sidpalas](https://community.thunkable.com/u/sidpalas)\
**Post date:** [July 23, 2018, 4:49pm UTC](https://community.thunkable.com/t/asynchronous-blocks-within-loops/33524/8 "2018-07-23T16:49:37Z")

</div>

Thanks Mark, I was running into this exact problem and solved it using your described approach! Are there any plans to add callbacks (or something equivalent) that could be used to trigger another set of blocks when an asynchonous block completes?

My use case involves uploading a set of photos to Cloudinary and storing the resulting mediaURLs into firebase. I have included a screenshot of the working block design with the hope it may help others.

 ![37%20AM](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/8/a/8a82b08911caf6096c4bb51c96f108080d7b09d8.png)

---

<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:** [July 23, 2018, 5:06pm UTC](https://community.thunkable.com/t/asynchronous-blocks-within-loops/33524/9 "2018-07-23T17:06:30Z")

</div>

Most of the blocks working asynchronously already have this mechanism. For example, “photoDB call Upload” or “RealtimeDB call Save” have a “then do” part, which is called after the action is completed. But you are right, some of the blocks do not have this mechanism, which complicates the development and leads to the need to use a timer.

---

<div class="post-metadata">

**Author:** ![Mark](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/mark/32/18133_2.png) [@Mark](https://community.thunkable.com/u/Mark)\
**Post date:** [July 23, 2018, 5:06pm UTC](https://community.thunkable.com/t/asynchronous-blocks-within-loops/33524/10 "2018-07-23T17:06:57Z")

</div>

Sid,

The `"then do"` slot **is** a callback slot. Blocks placed there are run when the asynchronous block completes.

-Mark

---

<div class="post-metadata">

**Author:** ![sidpalas](https://avatars.discourse-cdn.com/v4/letter/s/dbc845/32.png) [@sidpalas](https://community.thunkable.com/u/sidpalas)\
**Post date:** [July 23, 2018, 5:20pm UTC](https://community.thunkable.com/t/asynchronous-blocks-within-loops/33524/11 "2018-07-23T17:20:03Z")

</div>

Ah, right – but it can act up when used in loops? (hence this discussion thread)

---

<div class="post-metadata">

**Author:** ![Mark](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/mark/32/18133_2.png) [@Mark](https://community.thunkable.com/u/Mark)\
**Post date:** [July 23, 2018, 5:46pm UTC](https://community.thunkable.com/t/asynchronous-blocks-within-loops/33524/12 "2018-07-23T17:46:17Z")

</div>

As I mentioned in my original post:

> [@Mark](#):
>
> In the long run, we need to make it easier to deal with scenarios like this.

We are, in fact, working on some changes which might should help alleviate this problem. In the meantime, please use recursion if you need to serialize your asynchronous blocks (i.e. ensure that they run after one another) within a loop.

-Mark

---

<div class="post-metadata">

**Author:** ![sidpalas](https://avatars.discourse-cdn.com/v4/letter/s/dbc845/32.png) [@sidpalas](https://community.thunkable.com/u/sidpalas)\
**Post date:** [July 23, 2018, 5:53pm UTC](https://community.thunkable.com/t/asynchronous-blocks-within-loops/33524/13 "2018-07-23T17:53:04Z")

</div>

Got it – thanks for the quick replies @mark and @actech!

---

<div class="post-metadata">

**Author:** ![AcrobatEpee](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/acrobatepee/32/26336_2.png) [@AcrobatEpee](https://community.thunkable.com/u/AcrobatEpee)\
**Post date:** [October 2, 2018, 7:48pm UTC](https://community.thunkable.com/t/asynchronous-blocks-within-loops/33524/14 "2018-10-02T19:48:56Z")

</div>

Thanks Mark ! this was very helpful 🙂

---

<div class="post-metadata">

**Author:** ![Nenye](https://avatars.discourse-cdn.com/v4/letter/n/6a8cbe/32.png) [@Nenye](https://community.thunkable.com/u/Nenye)\
**Post date:** [February 27, 2019, 6:42am UTC](https://community.thunkable.com/t/asynchronous-blocks-within-loops/33524/15 "2019-02-27T06:42:44Z")

</div>

> [@Mark](#):
>
> this link

Hi Mark,

I’m unable to access the project through the link in your post. Please can you reconfirm the link and the permission of the project, and share an updated link.

Thanks

---

<div class="post-metadata">

**Author:** ![domhnallohanlon](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/domhnallohanlon/32/29554_2.png) [@domhnallohanlon](https://community.thunkable.com/u/domhnallohanlon)\
**Post date:** [May 11, 2019, 3:35pm UTC](https://community.thunkable.com/t/asynchronous-blocks-within-loops/33524/16 "2019-05-11T15:35:03Z")

</div>

Hi @Nenye, did you hit the “click to remix” link at the top of the project?

---

<div class="post-metadata">

**Author:** ![Nenye](https://avatars.discourse-cdn.com/v4/letter/n/6a8cbe/32.png) [@Nenye](https://community.thunkable.com/u/Nenye)\
**Post date:** [May 17, 2019, 5:50am UTC](https://community.thunkable.com/t/asynchronous-blocks-within-loops/33524/17 "2019-05-17T05:50:58Z")

</div>

Yeah, I did thanks

---

<div class="post-metadata">

**Author:** ![marton.tolvaj](https://avatars.discourse-cdn.com/v4/letter/m/eb9ed0/32.png) [@marton.tolvaj](https://community.thunkable.com/u/marton.tolvaj)\
**Post date:** [October 27, 2021, 5:37am UTC](https://community.thunkable.com/t/asynchronous-blocks-within-loops/33524/18 "2021-10-27T05:37:08Z")

</div>

Hi all

I have similar issues with a web API call that I would use to send customized emails.  
The idea is that first I get the list via an API call from Google sheets, then I use its content the customize the email address, and the body of the email (replacing names, dates, links etc.).  
Once the body is updated I would like to send the emails one by one to the different addresses with different content.  
I started to use a loop which did not work then I tried to use the same solution which is mentioned above but I was not able to solve my issue, and I hope that I missed something which you can catch.

This is how my script looks like:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/d/f/dfc5630b44b8e9c50d3acb30db41770ddea78478.png)

And this is the simple google script on the other end:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/7/1/711f82ba6fd3cef3338be1dd8466149cb25dd8d1.png)

When I use these blocks I send all the emails (72 emails) with the same email body, address, and subject and all of them go with the details of the first element in my list.

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/2/3/232e1e683e1966b5379c1b45c3492b46734362ee.png)

Just for your reference, I created a different block that sends 3 different emails and it is working perfectly:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/0/d/0df1ea00dfb4e4087a2d7c7414d9e94010a43b38.png)  
 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/9/d/9dc4ee7d7ca5eac92917bb48a0814feb4ecadb6a.png)

Do you have any idea how could I fix it?

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [October 27, 2021, 5:42am UTC](https://community.thunkable.com/t/asynchronous-blocks-within-loops/33524/19 "2021-10-27T05:42:09Z")

</div>

Your AsyncGmail function is going to run right after the SendEmail button is clicked. It’s not going to wait for the first API call to complete.

So since you’re depending on variable values set within the “then do” section of the first API call, you’re going to have to move that function call to the “then do” section.

---

<div class="post-metadata">

**Author:** ![marton.tolvaj](https://avatars.discourse-cdn.com/v4/letter/m/eb9ed0/32.png) [@marton.tolvaj](https://community.thunkable.com/u/marton.tolvaj)\
**Post date:** [October 27, 2021, 5:53am UTC](https://community.thunkable.com/t/asynchronous-blocks-within-loops/33524/20 "2021-10-27T05:53:20Z")

</div>

Sorry, I just realized that the first picture has been cut too early.  
I use the first call of asyncGmail to initiate the process.  
So this is how it looks like:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/a/8/a84ea4c5e91efe32cf379ea8c2cc5b653b981ddf.png)

So at the bottom, you can see that I call the recursion within the “then do” section of the email sending API post.

Please let me know If I still need to change something.

[Next page](https://community.thunkable.com/t/asynchronous-blocks-within-loops/33524.md?page=2)
