Using the back-end for a log time

Hello everyone,

I currently have this code running in the back end:

export function massReminderEmail() {
console.log("clicked");
wixData.query("2020_Registrants")
.ascending("givenName")
.limit(1000)
.find()
.then((results) => {
if (results.items.length > 0) {
let participant = results.items; //see item below
console.log(participant[0].givenName, "starting");
for (let i = 0, p = Promise.resolve(); i < results.items.length; i++) {
p = p.then(_ => new Promise(resolve =>
setTimeout(function () {
console.log(i, participant[i].fullName, participant[i].email);
sendPromoMail(participant[i].givenName, participant[i].email);

//$w('#massEmail').label = (i + 1) + " / " + results.items.length;
//no that this is in the back end i cant use this, how can i pass this data //back to edit the label still?

if(i===results.items.length-1)
{
sendPromoMail("yes", "me@mydomain.org"); //let me know when all emails sent
}
resolve();
}, 5000)
));
}
}
})
.catch((err) => {
let errorMsg = err;
});
}

This code is working perfectly fine. That is for at least the 12 database entries i have tested it with.
So the purpose of this code is it sends out a reminder email to each registrant in a database, but it puts a 5 second delay between each email call.

I call this backend function from a button click on the front end. I previously had this code running front end, but the browser had to be left open as each email sent. I want to avoid that by having it just done in the backend. (cause it could take up to an hour to send all the emails given a 5 second delay for each)

Anyway my main questions:

  1. Will this work for a lot more registrants (say 1000) or will the wix backend eventually just time out? I remember reading something about max compute time in wix backend.

  2. Is this the best way to have a delay between each email send? or can it be done safer/cleaner?

  3. How can I send data back to front end to update a text element as to the value of i to show what email number it is on.

Thank you all for your time.
I am getting used to using promises so please let me know if something could be done better.

-Logan

Hi Logan,

The function has to respond within 15 seconds. If you exceed this limit, you will get the following error: WebMethod request timed-out.
It’s not possible to extend this limit.

As for setting the delay, using timeout should be the best possible solution.

To show how many emails were send you need to return the “i” value after each iteration however, this won’t work as calling the return will basically terminate the function.
Though, if you do it on the front end, you can change the value of the relevant text field after each iteration.