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