.js function stops (loop)

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.

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:

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
}

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.

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.

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

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.

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

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 ?

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”

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

Doesn’t like the use a .map.

@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

@jonatandor35


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.