# Problems with For loop!

**URL:** <https://community.thunkable.com/t/problems-with-for-loop/35118>\
**Category:** Questions about Thunkable X\
**Created:** [July 10, 2018, 1:14pm UTC](https://community.thunkable.com/t/problems-with-for-loop/35118 "2018-07-10T13:14:29Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shakeel\_Hashim](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@Shakeel\_Hashim](https://community.thunkable.com/u/Shakeel_Hashim)\
**Post date:** [July 10, 2018, 1:14pm UTC](https://community.thunkable.com/t/problems-with-for-loop/35118/1 "2018-07-10T13:14:29Z")

</div>

Hi - I’m tearing my hair out with a problem and was wondering if anyone here might be able to help.

I have an Airtable spreadsheet that looks like this:

Name|Latitude|Longitude  
Bao|51.513256|-0.136554  
El Pastor|51.505315|-0.091193  
Xu|51.511144|-0.132614

I’m trying to create and display a list which shows the distance from an inputted location to each of these restaurants, and the name of each restaurant. I’m doing this using the Google Maps API.

The output I get from this in the app is a list of the correct distances to each restaurant, but it just repeats the name of the last restaurant 3 times. e.g.:

Xu 3248seconds  
Xu 177seconds  
Xu 2913seconds

The distances are correct, and displayed in the correct order. I just can’t figure out how to display the correct restaurant name for each distance! These are my blocks - if anyone can figure out what’s wrong here, that would be absolutely amazing. Thanks!

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/5/e/5e3da8d2dc200569215d84f4a2be15837f4db4a0.png)

To talk you through the blocks:

- first call the Airtable spreadsheet
- then begin loop for each item in the list
- the green Web\_API block is using the Google Maps, using the coordinates of each restaurant from the spreadsheet
- the purple API block gets the response; and then saves as a list the time in seconds to each restaurant (the big pink thing) and the name of the restaurant (this latter bit is what’s broken).

---

<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 10, 2018, 1:21pm UTC](https://community.thunkable.com/t/problems-with-for-loop/35118/2 "2018-07-10T13:21:50Z")

</div>

If you give a link to your project, then I’ll look it up.

---

<div class="post-metadata">

**Author:** ![Shakeel\_Hashim](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@Shakeel\_Hashim](https://community.thunkable.com/u/Shakeel_Hashim)\
**Post date:** [July 10, 2018, 1:24pm UTC](https://community.thunkable.com/t/problems-with-for-loop/35118/3 "2018-07-10T13:24:02Z")

</div>

[https://x.thunkable.com/copy/039fec8f1abbaf6ac9787a1d51afb1f3](https://x.thunkable.com/copy/039fec8f1abbaf6ac9787a1d51afb1f3) - 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:** [July 10, 2018, 1:24pm UTC](https://community.thunkable.com/t/problems-with-for-loop/35118/4 "2018-07-10T13:24:23Z")

</div>

Ok

---

<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 10, 2018, 1:42pm UTC](https://community.thunkable.com/t/problems-with-for-loop/35118/5 "2018-07-10T13:42:09Z")

</div>

The problem is that the Web\_API.Get uses slow asynchronous call. A cycle has worked, as Web\_API.Get have not had time for it and, therefore, displayed the latest reading. You will need to modify the algorithm so that the next call to Web\_API.Get happened only after the completion of the previous call.

Here it is necessary to use recursion, as Mark showed.

> [@Asynchronous Blocks within Loops](http://community.thunkable.com/t/asynchronous-blocks-within-loops/33524):
>
> 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 (lik…

I will think over such a block

---

<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 10, 2018, 1:59pm UTC](https://community.thunkable.com/t/problems-with-for-loop/35118/6 "2018-07-10T13:59:54Z")

</div>

Check it

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

---

<div class="post-metadata">

**Author:** ![Shakeel\_Hashim](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@Shakeel\_Hashim](https://community.thunkable.com/u/Shakeel_Hashim)\
**Post date:** [July 10, 2018, 5:39pm UTC](https://community.thunkable.com/t/problems-with-for-loop/35118/7 "2018-07-10T17:39:01Z")

</div>

Amazing - seems to work perfectly. Thanks so much for this - absolute lifesaver!

---

<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 26, 2019, 11:02pm UTC](https://community.thunkable.com/t/problems-with-for-loop/35118/8 "2019-02-26T23:02:51Z")

</div>

Please can you share how you resolved the issue. I have a similar issue with my own project.

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:** [February 28, 2019, 7:10am UTC](https://community.thunkable.com/t/problems-with-for-loop/35118/9 "2019-02-28T07:10:54Z")

</div>

Unfortunately, the project for this link I have not survived. If you give a link to your project, I will try to see it.

---

<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:** [March 2, 2019, 10:33pm UTC](https://community.thunkable.com/t/problems-with-for-loop/35118/10 "2019-03-02T22:33:15Z")

</div>

Thanks!

Here’s the link- [https://x.thunkable.com/projects/5b60e879b159a33c851ac8c9](https://x.thunkable.com/projects/5b60e879b159a33c851ac8c9)

---

<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:** [March 3, 2019, 5:23am UTC](https://community.thunkable.com/t/problems-with-for-loop/35118/11 "2019-03-03T05:23:49Z")

</div>

After clicking on your link, I see a blank white screen.

---

<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:** [March 3, 2019, 6:59am UTC](https://community.thunkable.com/t/problems-with-for-loop/35118/12 "2019-03-03T06:59:15Z")

</div>

I just made it public. Please, can you try again?

[https://x.thunkable.com/projects/5b60e879b159a33c851ac8c9/Home/designer](https://x.thunkable.com/projects/5b60e879b159a33c851ac8c9/Home/designer)

---

<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:** [March 3, 2019, 7:17am UTC](https://community.thunkable.com/t/problems-with-for-loop/35118/14 "2019-03-03T07:17:40Z")

</div>

Project opened

---

<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:** [March 3, 2019, 7:19am UTC](https://community.thunkable.com/t/problems-with-for-loop/35118/15 "2019-03-03T07:19:18Z")

</div>

On which screen and in which block is the code that does not work correctly? There are a lot of duplicate blocks in your code, an incomprehensible delay of 10 seconds, a lot of unnecessary variables, no checks for errors when getting data from the API. Try to simplify the code. The more blocks - the more mistakes.

---

<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, 12:02pm UTC](https://community.thunkable.com/t/problems-with-for-loop/35118/16 "2024-11-08T12:02:06Z")

</div>


