Backend Function Call Timeout

I have this simple front-end function xyz that calls a backend query function (with all of the appropriate import statements and onReady sections and such):

async function xyz() {
 let tempProd = await get_my_product("Prod_04");
 let myNewVariable = tempProd.price;
 }

My backend query function looks like this:

export async function get_my_product(prodId) {
 return wixData.query("products")
        .eq("productId", prodId)
        .limit(1)
        .find()
        .then((results) => {
            let prodItems = results.items;
            return (prodItems[0]);
        })
        .catch((error) => {
        let errorMsg = error.message;
        myLog("My Product Query Error Msg/Code = " + errorMsg)
        });
}

This works “almost” all of the time (actually I have dozens of these back-end query functions and maybe hundreds of the front-end function calls to them throughout my system). I am pretty sure the basic format is correct since my system works in general, however I think that I am missing something related to Promises or back-end timeouts. Several times per day, when the internet seems to slow down, my testers run into the following error from the front end:

Uncaught (in promise) TypeError: Cannot read property ‘price’ of undefined

I am guessing this is thrown because the Promise is not being resolved and my guess is that it is because the Backend Server and/or the DB server is not responding and is timing out based on the following back-end myLog data I am collecting.

My Product Query Error Msg/Code = WDE0028: Request timed out. / WD_REQUEST_TIMED_OUT

My question is how do I best trap this in my code and what is the best course of action if this happens? Should I trap it in the front-end code or in the back-end code? I need to keep trying until the back-end servers give me a response. How should I best do this?

Any advice would be most appreciated!
Thanks,
Jay

If I were you I would look online for how to implement retry mechanism for promise. That’s a generic issue and you’ll find better answer but here is how I would do it:

https://gist.github.com/plomteuxquentin/26d06e5aa6231064540f861812114b5c

(I’ve not test the code and comments are welcome)

let me know if it helps

Thanks Quentin! I will certainly give this a try and let you know how it works. I agree with you that I might have to move this to the front end code in case the Backend server access is causing a timing issue. Non-the-less, I will try it out on my query functions and see if it helps with stability.