Skip to content

Instantly share code, notes, and snippets.

@plomteuxquentin
Last active June 16, 2020 04:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save plomteuxquentin/26d06e5aa6231064540f861812114b5c to your computer and use it in GitHub Desktop.
Save plomteuxquentin/26d06e5aa6231064540f861812114b5c to your computer and use it in GitHub Desktop.
Retry query for wix
//This is an answer for the following Wix post https://www.wix.com/corvid/forum/community-discussion/backend-function-call-timeout
// Code hasn't been tested and might require ajustment. Plz comment below
const TIMEOUT_CODE= '...'; // error message from wix server
const RETRY_MAX = 3;
// Note that this will work as long as your frontend Promise does not expire.
// It might be safer to have the retry on the frontend side.
function retryQuery(query, iteration=0){
return query.find().catch(reason => {
if(reason === TIMEOUT_CODE) {
if(iteration === RETRY_MAX){
console.error('retried '+RETRY_MAX+' times and failed');
throw reason;
}
return retryQuery(query, ++iteration);
}
//Not a timeout, throw back the error
throw reason;
})
}
//Usage
function get_my_product(prodId) {
let query = wixData.query("products").eq("productId", prodId).limit(1);
return retryQuery(query)
.then((results) => {
let prodItems = results.items;
return (prodItems[0]);
}).catch(reason => {
let errorMsg = error.message;
myLog("My Product Query Error Msg/Code = " + errorMsg)
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment