javascript 承诺一次一个?

x0fgdtte  于 2023-01-16  发布在  Java
关注(0)|答案(1)|浏览(113)

因此,我似乎不是很理解承诺,但我一直在我的公司用于内部工具的低代码软件中使用它们,作为一种对不同数据执行一定次数的相同查询的方式。
无论如何,我现在正在使用Promises和Mailgun查询,当我尝试解析Promise.all(promises)时,我认为我对它们的处理太快太多了。所以我想做的是,不需要重构我的整个代码,然后一次一个地解析这些Promises。

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);

我试着用不同的方法去了解承诺,我试着拼接承诺并解决一个,我想我唯一学到的是我不理解承诺。
有人有什么想法吗?

xytpbqjk

xytpbqjk1#

两件事:

  • 你的finalResult.map((item) => {似乎没有返回任何承诺,就像TJ解释的那样,我认为你的意思是用任何一种方式来做return query.trigger,Map都会立即运行(并且是并行的),所以你写的函数实际上不会等待任何东西,所以可能是其他对你的函数的链接调用会被立即调用,b/c Promise.all实际上不会等待任何东西。

let promises =似乎是一个未定义值的数组,所以Promise.all(promises)同样对您没有任何作用。

  • 如果你想一次运行一个,那么删除finalResult.map((item) =>,使用类似经典的for循环和async/await:
for (const item of finalResult) {
  await query.trigger(...)
}

如果你想使用await,你的函数需要有async关键字,
async function foo() { ... }

相关问题