# How do I use the API connector appropriately in a “For Each” loop?

**URL:** https://community.thunkable.com/t/how-do-i-use-the-api-connector-appropriately-in-a-for-each-loop/1838221
**Category:** Questions about Thunkable X
**Tags:** webapi
**Created:** [April 19, 2022, 11:56am UTC](https://community.thunkable.com/t/how-do-i-use-the-api-connector-appropriately-in-a-for-each-loop/1838221 "2022-04-19T11:56:55Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![assistsolucoes](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/assistsolucoes/32/128709_2.png) [@assistsolucoes](https://community.thunkable.com/u/assistsolucoes)
#### Post date: [April 19, 2022, 11:56am UTC](https://community.thunkable.com/t/how-do-i-use-the-api-connector-appropriately-in-a-for-each-loop/1838221/1 "2022-04-19T11:56:55Z")

</div>

I am working with data source, using Local Database where i use foreach list on all Ids of the database.  
if i add the block delete in datasource, on return of web api the delete dont work.

Follow print in attachment

The foreach  
 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/6/8/686ddc15d3f8ff80d9503585d5a73119a3260a4d.png)

Don’t work  
 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/1/1/1162d57514867b104a2997c959b433a7a0bc4111.png)

this way it works, but I need to validate the API return.  
 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/6/4/6423288346ebc270bc81eb1155c1b318ef042b4c.png)

---

<div class="post-metadata">

### Author: ![muneer](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/muneer/32/75210_2.png) [@muneer](https://community.thunkable.com/u/muneer)
#### Post date: [April 19, 2022, 12:16pm UTC](https://community.thunkable.com/t/how-do-i-use-the-api-connector-appropriately-in-a-for-each-loop/1838221/2 "2022-04-19T12:16:15Z")

</div>

This is a synchronization issue. The real issue that you are facing is that the `Web API` is very much slower that the `ForEach` loop. What actually happens is that the loop is faster than the `Web API` and it goes into the second and third loop before the API respond.

See this demo  
[https://x.thunkable.com/projectPage/6163132fd2994700101957c2](https://x.thunkable.com/projectPage/6163132fd2994700101957c2)

---

<div class="post-metadata">

### Author: ![assistsolucoes](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/assistsolucoes/32/128709_2.png) [@assistsolucoes](https://community.thunkable.com/u/assistsolucoes)
#### Post date: [April 19, 2022, 12:42pm UTC](https://community.thunkable.com/t/how-do-i-use-the-api-connector-appropriately-in-a-for-each-loop/1838221/3 "2022-04-19T12:42:07Z")

</div>

Thanks, i understood!  
If i add a timer before execute delete the problem can is solved?  
Or have other solution for this?

---

<div class="post-metadata">

### Author: ![jared](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/jared/32/138473_2.png) [@jared](https://community.thunkable.com/u/jared)
#### Post date: [April 19, 2022, 12:45pm UTC](https://community.thunkable.com/t/how-do-i-use-the-api-connector-appropriately-in-a-for-each-loop/1838221/4 "2022-04-19T12:45:18Z")

</div>

One thing that I would consider is creating a recursive function. A recursive function is a function that calls itself until its planned execution is complete. Are you familiar with that concept?

In this type of set up you could call the function which calls the API and in the vendue section you can call the function again. This ensures the function/api call is not ran again until the first API call is complete.

- note: using recursive functions can lead to issues when you are dealing with very large data sets OR when you don’t know the length of the list to iterate upon (i.e. Your app needs to know how many times to run the function before moving on to other tasks). If you don’t know the length of the. data set, your function could run forever leading to a crash. If you are dealing with large datasets and do know the number, you may want to block the user from further interacting with the app until the entire task is finished.

---

<div class="post-metadata">

### Author: ![assistsolucoes](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/assistsolucoes/32/128709_2.png) [@assistsolucoes](https://community.thunkable.com/u/assistsolucoes)
#### Post date: [April 19, 2022, 12:55pm UTC](https://community.thunkable.com/t/how-do-i-use-the-api-connector-appropriately-in-a-for-each-loop/1838221/5 "2022-04-19T12:55:25Z")

</div>

I never used this function.  
But in the munner demo there is an example.  
I will test this recommendation and i return with an answer.

---

<div class="post-metadata">

### Author: ![muneer](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/muneer/32/75210_2.png) [@muneer](https://community.thunkable.com/u/muneer)
#### Post date: [April 19, 2022, 1:34pm UTC](https://community.thunkable.com/t/how-do-i-use-the-api-connector-appropriately-in-a-for-each-loop/1838221/6 "2022-04-19T13:34:47Z")

</div>

See this demo to solve the issue in the previous demo  
[https://x.thunkable.com/projectPage/6174ffbb20566a0011faf503](https://x.thunkable.com/projectPage/6174ffbb20566a0011faf503)

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/0/f/0f59f292712368bce16ff80133ae10de8a6f27e8.png) [@system](https://community.thunkable.com/u/system)
#### Post date: [July 18, 2022, 1:35pm UTC](https://community.thunkable.com/t/how-do-i-use-the-api-connector-appropriately-in-a-for-each-loop/1838221/7 "2022-07-18T13:35:40Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
