簡體   English   中英

一次承諾一個?

[英]Promises One At A Time?

所以我似乎不太了解承諾,但我一直在我公司用於內部工具的低代碼軟件中使用它們,作為對不同數據執行相同查詢一定次數的一種方式。

無論如何,我目前正在將 Promises 與 Mailgun 查詢一起使用,當我嘗試解析 Promise.all(promises) 時,我認為我擊中它們的速度太快且太多了。 因此,我想做的是,無需重構我的全部代碼,而是利用我所擁有的,然后一次解決這些 Promise。

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

我嘗試在 Promise 上使用不同的方法來查看會發生什么,我嘗試拼接 Promises 並解決一個。 我認為我學到的唯一一件事就是我不理解 Promises。

有人有什么想法嗎?

2件事:

  • 你的finalResult.map((item) => {似乎沒有像 TJ 解釋的那樣返回任何 promise。我想你打算做return query.trigger map 立即(並行)運行的任何一種方式所以你寫的 function並沒有真正等待任何事情,因此可能會立即調用對您的 function 的其他鏈接調用 b/c Promise.all並沒有真正等待任何事情。

let promises =似乎是一個未定義值的數組? 所以再次Promise.all(promises)什么都不給你。

  • 如果你想一次運行一個,然后刪除finalResult.map((item) =>並使用類似於經典 for 循環的東西並使用 async/await:
for (const item of finalResult) {
  await query.trigger(...)
}

如果你想使用 await,你的 function 需要有 async 關鍵字,
async function foo() {... }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM