Throttle Query requests generated by JavaScript Query w Promises

I am sending a batch of emails using the following (redacted/simplified) JS Query:

const arr = PaymentList.value //your array of things to iterate over
const query = SendRemitance // your query to trigger for each

// Tip: assign your external references to variables instead of chaining off the curly brackets.
function eMailSubject(nm, amt) {return "Payment has been made by X for " + nm +" for GBP "+ amt}
function eMailBody(personNm, amt, inv) {
  var tmplt = "<html lang=\"en\"> ... </html>"
  var nwInv = InvoicesTable(inv,amt)
  var nwNm = (personNm==null||personNm.length==0?"":" "+personNm)
  return tmplt.replace("@@name@@",nwNm).replace("@@Ammount@@",amt).replace("@@table@@",nwInv)
}
function InvoicesTable(inv, amt) {
  var start = "<table><thead><tr><th style=\"padding:10px\">Invoice Date</th><th style=\"padding:10px\">Reference</th><th style=\"text-align:right; padding:10px\" >Invoice Total</th><th style=\"text-align:right; padding:10px\">Amount Paid</th><th style=\"text-align:right; padding:10px\">Still Owing</th></tr></thead><tbody>"
  var rw = "<tr><td style=\"padding:10px\" >invdt</td><td style=\"padding:10px\">ref</td><td style=\"text-align:right; padding:10px\">total</td><td style=\"text-align:right; padding:10px\">paid</td><td style=\"text-align:right; padding:10px\">0.00</td></tr>"
  var end = "</tbody><tfoot><tr><th></th><th></th><th style=\"text-align:right; padding:10px\">Total GBP</th><th style=\"text-align:right; padding:10px\">amt</th><th style=\"text-align:right; padding:10px\">0.00</th></tr></tfoot></table>"
  
  var dt = ""
  for (var i in inv) {
    var ln = inv[i]
    var dit = ln.dt.substring(0,10)
    var row = rw.replace("invdt",dit).replace("ref",ln.ref).replace("total",ln.total).replace("paid",ln.amount)
    dt = dt + row
  }
  
  return start +dt+end.replace("amt", amt)}


const promises = arr.map((item) => {
//var item = arr[0]  
var to = item.detail.EmailAddress
  var bsnm = item.detail.Name
  var amt = item.amount
  var inv = item.InvIDs
  var fn = item.FirstName
  
  var sub = eMailSubject(bsnm, amt)
  var body = eMailBody(fn,amt,inv)
  var content ={}
  content.subject = sub
  content.body={}
  content.body.contentType = "HTML"
  content.body.content = body
  content.toRecipients=[]
  var toO = {}
  toO.emailAddress ={}
  toO.emailAddress.name=to
  toO.emailAddress.address=to 
  content.from = {"emailAddress":{"name":"me","address": "me@work.com"}}
  content.toRecipients.push(toO)
  
  return  query.trigger({
      additionalScope: {
        email:content
      }
   });
 
 });

return Promise.all(promises)

Now it all works fine for small arr / PaymentList.value but for larger lists the SendRemitance query fails due to throttling on the provider end.

I feel like I should be able to throttle calls in ReTool to SendRemitance but I can't seem to find the option. Am I missing something?

Hey @mawdo81! How big is a typical larger list that doesn't seem to be working?

Would something like this be helpful?

https://community.retool.com/t/how-to-run-a-large-array-of-queries-in-smaller-batches/3170

Thanks Victoria, we're not talking large lists here, between 3 & 10 so I don't think batching is the issue tbh.

Got it. Does the query fail with any error message in particular?

It tends to pop up as a notification on the top of the screen then disappear, so not sure. Will try to remember to record next time.

Hey @mawdo81, if you are hitting rate limits on the provider side batching is probably your best bet here. There is no throttle setting on individual queries, so you'd have to handle that yourself in the JS query.

Feel free to post that error here when you see it again, happy to talk through ways to avoid it.