# .js function stops (loop)

**URL:** <https://forum.wixstudio.com/t/js-function-stops-loop/6212>\
**Category:** Ask the community\
**Tags:** code, question\
**Created:** [February 17, 2022, 10:52pm UTC](https://forum.wixstudio.com/t/js-function-stops-loop/6212 "2022-02-17T22:52:47Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul51482](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/paul51482/32/31597_2.png) [@paul51482](https://forum.wixstudio.com/u/paul51482)\
**Post date:** [February 17, 2022, 10:52pm UTC](https://forum.wixstudio.com/t/js-function-stops-loop/6212/1 "2022-02-17T22:52:47Z")

</div>

Basically I have a http-function hook triggered externally that runs a function. The function periodically just stops mid way through execution and I don’t know why. I’ve re-written the code about 3 times and added debugging to attempt to capture and pin point the issue with no luck.

Here is a basic code snippet of where I think it’s falling over.

**export async function** function\_in ( inputs …) {

```
**let** users = **await** users () 

// loop through user list 
**for** ( **let** u = 0 ; u < users.length ; u ++) { 
    //code 

    **let** x = **await** order ( users.items[u]. id ) 

    console . log ( x ) 
} 

```

}  
So if users() returns say 10 items, the code will run and just stop at item 4 for example. I’ve checked my usage on async/await.  
Can’t find much help online.

---

<div class="post-metadata">

**Author:** ![J.D](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/j.d/32/7312_2.png) [@J.D](https://forum.wixstudio.com/u/J.D)\
**Post date:** [February 17, 2022, 11:17pm UTC](https://forum.wixstudio.com/t/js-function-stops-loop/6212/2 "2022-02-17T23:17:41Z")

</div>

I don’t know. (are you sure it’s .id and not .\_id ? )  
If there’re not too many users you can try running them in parallel without the for-loop:

```javascript
export async function function_in(inputs...) {
 const users = await users();
 const userOrder = await Promise.all(users.map(u => order(u.id))); 
 //userOrder will be an array with all the results
}

```

---

<div class="post-metadata">

**Author:** ![amotor](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@amotor](https://forum.wixstudio.com/u/amotor)\
**Post date:** [February 17, 2022, 11:32pm UTC](https://forum.wixstudio.com/t/js-function-stops-loop/6212/3 "2022-02-17T23:32:41Z")

</div>

Do you get any errors when you inspect the network tab? The code you provided does not contain any obvious errors so I am wondering if all the requests are being made and returned successfully.

---

<div class="post-metadata">

**Author:** ![paul51482](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/paul51482/32/31597_2.png) [@paul51482](https://forum.wixstudio.com/u/paul51482)\
**Post date:** [February 17, 2022, 11:41pm UTC](https://forum.wixstudio.com/t/js-function-stops-loop/6212/4 "2022-02-17T23:41:38Z")

</div>

Yep it’s def. .id.

I need the for loop because it contains a lot more than the call to order().

this is where I think it’s falling over though. There’s quite a few await function calls inside the loop actually.

I don’t understand though. 90% of the time it works, then it just stop.

---

<div class="post-metadata">

**Author:** ![paul51482](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/paul51482/32/31597_2.png) [@paul51482](https://forum.wixstudio.com/u/paul51482)\
**Post date:** [February 17, 2022, 11:42pm UTC](https://forum.wixstudio.com/t/js-function-stops-loop/6212/5 "2022-02-17T23:42:04Z")

</div>

It’s a backend function but I can’t see any errors anywhere.

---

<div class="post-metadata">

**Author:** ![amotor](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@amotor](https://forum.wixstudio.com/u/amotor)\
**Post date:** [February 17, 2022, 11:46pm UTC](https://forum.wixstudio.com/t/js-function-stops-loop/6212/6 "2022-02-17T23:46:44Z")

</div>

you mentioned an HTTP function. Are you interacting with an API? Or making external requests? If so what is the status of those requests in the network tab when you inspect the page and then call the function? You should see the request and get a response back.

---

<div class="post-metadata">

**Author:** ![paul51482](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/paul51482/32/31597_2.png) [@paul51482](https://forum.wixstudio.com/u/paul51482)\
**Post date:** [February 17, 2022, 11:52pm UTC](https://forum.wixstudio.com/t/js-function-stops-loop/6212/7 "2022-02-17T23:52:09Z")

</div>

I’m POSTing to http-functions which responds OK and it passes the json data to this function that is stopping. I’m confident it’s not that.

So I POST once, then it will loop through users() and sometimes it just stops.

const userOrder = await Promise . all ( users . map ( u =\> order (u. id )));  
This line of code does interest me, I may be able to use that to remove all my await calls outside of the loop. ?

---

<div class="post-metadata">

**Author:** ![paul51482](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/paul51482/32/31597_2.png) [@paul51482](https://forum.wixstudio.com/u/paul51482)\
**Post date:** [February 17, 2022, 11:57pm UTC](https://forum.wixstudio.com/t/js-function-stops-loop/6212/8 "2022-02-17T23:57:38Z")

</div>

**export async function** function\_in ( inputs …) {

```
**let** users = **await** users ()       
let userOrder = await [Promise](Promise.all(users.map(u=>order(u.id)));) [.](Promise.all(users.map(u=>order(u.id)));) [all](Promise.all(users.map(u=>order(u.id)));) [(](Promise.all(users.map(u=>order(u.id)));) [users](Promise.all(users.map(u=>order(u.id)));) [.](Promise.all(users.map(u=>order(u.id)));) [map](Promise.all(users.map(u=>order(u.id)));) [(](Promise.all(users.map(u=>order(u.id)));) [u](Promise.all(users.map(u=>order(u.id)));) [=>](Promise.all(users.map(u=>order(u.id)));) [order](Promise.all(users.map(u=>order(u.id)));) [(u.](Promise.all(users.map(u=>order(u.id)));) [id](Promise.all(users.map(u=>order(u.id)));) [)));](Promise.all(users.map(u=>order(u.id)));)  

// loop through user list 
**for** ( **let** u = 0 ; u < users.length ; u ++) {          
	//code 

	// **let** x = **await** order ( users.items[u]. id )           

	console . log (userOrder[u])      
} 

```

}

something like that I’m assuming ?

---

<div class="post-metadata">

**Author:** ![amotor](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@amotor](https://forum.wixstudio.com/u/amotor)\
**Post date:** [February 17, 2022, 11:58pm UTC](https://forum.wixstudio.com/t/js-function-stops-loop/6212/9 "2022-02-17T23:58:43Z")

</div>

It was that order function call that I was referring to in my previous responses. It is impossible to isolate further without more context or information.

The reason I asked to inspect the network tab specifically is because in Wix you can only make a maximum of 10 concurrent requests. If you exceed that (which could happen in the event that a function returns a promise or starts a call without completing it) than it does not matter if you nessasarally await the response of that function as it would return a promise anyways.

If this was happening you will see an error in the network tab and when viewing the response of the bad request it would say something like “error max requests threshold reached”

---

<div class="post-metadata">

**Author:** ![paul51482](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/paul51482/32/31597_2.png) [@paul51482](https://forum.wixstudio.com/u/paul51482)\
**Post date:** [February 18, 2022, 12:03am UTC](https://forum.wixstudio.com/t/js-function-stops-loop/6212/10 "2022-02-18T00:03:34Z")

</div>

@amotor oh ok. I’m not keen on sharing all my source code publicly. Is it possible to share it privately?

There is a function call before it stops that does access an external API, but that returns OK or else it would not fall through.

The order() functions is a basic query to a collection using an async function and an await on the wixdata query.

---

<div class="post-metadata">

**Author:** ![paul51482](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/paul51482/32/31597_2.png) [@paul51482](https://forum.wixstudio.com/u/paul51482)\
**Post date:** [February 18, 2022, 12:58am UTC](https://forum.wixstudio.com/t/js-function-stops-loop/6212/11 "2022-02-18T00:58:06Z")

</div>

Doesn’t like the use a .map.

![](https://us1.discourse-cdn.com/wix/original/3X/f/0/f05cb909ac6da382c471dfb643bb29e32387c2bd.png)

![](https://us1.discourse-cdn.com/wix/original/3X/7/0/7055fe83a7855ade8b20fdfa462a4050473f898f.png)

---

<div class="post-metadata">

**Author:** ![J.D](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/j.d/32/7312_2.png) [@J.D](https://forum.wixstudio.com/u/J.D)\
**Post date:** [February 18, 2022, 1:25am UTC](https://forum.wixstudio.com/t/js-function-stops-loop/6212/12 "2022-02-18T01:25:47Z")

</div>

@paul51482 it looks like the server\_users function returns queryResults, but you need the **queryResults.items** (for your for-loop or for the .map method) + if it’s query results items maybe you meant .\_id and not .id

---

<div class="post-metadata">

**Author:** ![paul51482](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/paul51482/32/31597_2.png) [@paul51482](https://forum.wixstudio.com/u/paul51482)\
**Post date:** [February 18, 2022, 2:21am UTC](https://forum.wixstudio.com/t/js-function-stops-loop/6212/13 "2022-02-18T02:21:33Z")

</div>

@jonatandor35

![](https://us1.discourse-cdn.com/wix/original/3X/3/6/360a92a05c53f9ea753032247cf41513ad7de981.png)  
Thankyou, yep that works nice.

I 100% mean .id and NOT .\_id.

I’ll create an implementation using this ‘method’ and see how we go.

Thankyou that was VERY hepful.
