Problems with For loop!

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!

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).

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

https://x.thunkable.com/copy/039fec8f1abbaf6ac9787a1d51afb1f3 - thanks!

Ok

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.

I will think over such a block

1 Like

Check it

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

1 Like

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

1 Like

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

Thanks

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

Thanks!

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

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

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

https://x.thunkable.com/projects/5b60e879b159a33c851ac8c9/Home/designer

Project opened

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.