I’ve built a light-weight queue management service using Google Firestore and Netlify’s serverless functions.
There’s a function called “queue_next.js” that checks whether we can place the next order, and if we can, it places an order. The last step should be calling itself so the queue continues to be processed. That last step – calling itself via Axios – is called asynchronously, but not using await so the current function can return and just spawn itself again.
When developing locally it’s working like a charm: an order gets placed, the queue processes the next job up, and calls itself at the last second right before returning the previous result. No timeout, no problem.
When deployed on Netlify in production, however, it’s very inconsistent. Sometime the recursive call makes it’s way through once or more times, sometimes it just stalls completely. There’s nothing in my logs that shows any issues and I’m struggling to figure it out.
I’m wondering if – because I’m not suings await – that in some cases the function returns before that last moment call is sent, but I have no way of testing.
Ok, so to help anyone in future experiencing the same thing, I got around this by introducing a delay after making a recursive request.
In my application, I have a wrapper function that either calls a netlify function directly (in dev environment) or makes a fetch request to the function (in production). I now await this wrapper function, and add a delay after the fetch (which is not awaited), and lambda-lambda invocations now seem to be working. This is analogous to adding an await Promise.resolve() after the Axios.get(...) call in the OP’s example.
Thanks so much for reaching out and sharing that! Knowledge sharing is part of what make the Forums great, and we appreciate you setting other members up for success.
Is there any fixes to this? this still seems to be a problem and awaiting the response from one function to another is not going to work for our use-case.
We have two functions
Function A will run on a schedule (now using the new scheduled function)
It fetches 1-100 documents from a database
Then loops through those documents and calls function B with each data point.
Problem is, in production it seems like function B is called maybe 80-90% of the time.
In development it runs perfectly every single time.
I’d personally advise to use recursive calls from the front-end rather than doing it in the function. For example, take a look at this code (not the best code, but does the job):