So it seems I don't quite understand promises, but I've been using them in low code software my company uses for internal tools as a way to perform the same query on different data for a certain number of times.
Anyway, I'm currently using Promises with a Mailgun query, and when I try to resolve Promise.all(promises), I assume I'm hitting them too quickly and too much. So what I would like to do, without having to refactor the entirety of my code, is take what I have and then resolve those Promises one at a time.
let query = Mailgun_MailList_Add_Members;
//let arr = testEmailData.value;
let reps = repInfo.value;
let tableData = table1.selectedRow.data;
let finalResult = [];
for(let i = 0; i < reps.length; i++){
let emailArr = [];
let allRepEmails = [];
/* function that takes an array and checks inside for subarrays, pushing all subvalues into one new array */
let getAllRepEmails = (arr) => {
if(arr instanceof Array){
for(let i = 0; i < arr.length; i++){
getAllRepEmails(arr[i]);
}
}
else allRepEmails.push(arr);
}
for(let j = 0; j < tableData.length; j++){
/* check if current records owningrep is equal to current index of repinfos lastName */
if(tableData[j].owningrep.toUpperCase() == reps[i].lastName.toUpperCase()){
/* takes all the emails from table data in the crrent index and pushes them into array */
emailArr.push(tableData[j].Emails.replace(/;/g, ",").replace(/:/g, ",").replace(/ +/g, "").replace(/,+/g, ",").split(','));
}
}
/* check inside emailArr for subarrays of emails, pushing emails into new array */
getAllRepEmails(emailArr);
/* filters array of all emails for current rep to not include empty strings */
let noEmptyEmails = _.filter(allRepEmails, el => el != "");
/* loops over final array of all actual emails, creating objects for each rep with arrays of emails up to 1000 each per API req and pushing them into final array */
while(noEmptyEmails.length){
finalResult.push({
owningrep: reps[i].lastName.toUpperCase(),
/* converts final email array into JSON format as per API req */
Emails: JSON.stringify(noEmptyEmails.splice(0,1000))
});
}
}
/* maps finalResults to create the promises that perform the query for each record */
let promises = finalResult.map((item) => {
/* get lastName from repinfo for address variable */
let name = _.filter(repInfo.value, obj => obj.lastName == item.owningrep)[0].lastName.toLowerCase();
/* uses name variable and repinfo fromAddress to make address variable representing alias for the mail list we are adding members to */
let address = _.filter(repInfo.value, obj => obj.lastName == item.owningrep)[0].fromAddress.replace(/^[^@]*/, name + "test");
query.trigger({
additionalScope: {
members: finalResult[finalResult.indexOf(item)].Emails,
alias: address
}
})
}
);
return Promise.all(promises);
I'm tried using the different methods on Promise to see what happens, I've tried splicing Promises and resolving one. I think the only thing I've learned is that I don't understand Promises.
Does anyone have any ideas?