亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

嵌套 Promise.all 異步/等待映射

嵌套 Promise.all 異步/等待映射

慕容森 2022-10-08 10:35:51
我遇到了一個嵌套的 for 循環,其中包含等待操作,如下所示:    async handleProjects (projects) {        for (let i = 0; i < projects.rows.length; i++) {            projects.rows[i].owner = await this.userProvider.serialize(projects.rows[i].owner);            for (let j = 0; j < projects.rows[i].collaborators.length; j++) {                const collaborator = await this.userProvider.serialize(projects.rows[i].collaborators[j].email);                if (collaborator) {                    projects.rows[i].collaborators[j].name = collaborator.name;                    delete projects.rows[i].collaborators[j].role;                }            }        }        return projects;    }1. 上面的代碼是按順序運行的嗎?2. 為了提高性能我想使用 promise.all 如下所示,但有些運行時間大致相同,有些時候 promise.all 甚至更長。我的錯誤在哪里?    async handleProject (projects) {        await Promise.all(projects.rows.map(async (row) => {            console.log(row);            row.owner = await this.userProvider.serialize(row.owner);            return await Promise.all(row.collaborators.map(async (collaborator) => {                const collaboratorObj = await this.userProvider.serialize(collaborator.email);                if (collaboratorObj) {                    collaborator.name = collaboratorObj.name;                    delete collaborator.role;                }            }));        }));        return projects;    }
查看完整描述

1 回答

?
叮當貓咪

TA貢獻1776條經驗 獲得超12個贊

讓我們看一下使用超時來模擬您的異步調用。

在您進行優化之前,此代碼等效于您的第一個示例。請注意在任何給定時刻只有一個承諾待處理:

let serializeAndCache = owner => {

  console.log(`Starting: ${owner}`);

  let prm = new Promise(r => setTimeout(r, 2000));

  prm.then(() => console.log(`Finished: ${owner}`));

  return prm;

};


let project = {

  rows: [

    {

      owner: 'owner1',

      collaborators: [

        { name: null, email: '[email protected]' },

        { name: null, email: '[email protected]' },

        { name: null, email: '[email protected]' },

        { name: null, email: '[email protected]' }

      ]

    },

    {

      owner: 'owner2',

      collaborators: [

        { name: null, email: '[email protected]' },

        { name: null, email: '[email protected]' },

        { name: null, email: '[email protected]' },

        { name: null, email: '[email protected]' }

      ]

    },

    {

      owner: 'owner3',

      collaborators: [

        { name: null, email: '[email protected]' },

        { name: null, email: '[email protected]' },

        { name: null, email: '[email protected]' },

        { name: null, email: '[email protected]' }

      ]

    }

  ]

};


(async () => {

  for (let row of project.rows) {

    row.owner = await serializeAndCache(row.owner);

    for (let collaborator of row.collaborators) {

      let c = await serializeAndCache(collaborator.email);

      if (!c) continue;

      collaborator.name = c.name;

      delete collaborator.role;

    }

  }

})();

這段代碼相當于你的優化版本:

let serializeAndCache = owner => {

  console.log(`Starting: ${owner}`);

  let prm = new Promise(r => setTimeout(r, 2000));

  prm.then(() => console.log(`Finished: ${owner}`));

  return prm;

};


let project = {

  rows: [

    {

      owner: 'owner1',

      collaborators: [

        { name: null, email: '[email protected]' },

        { name: null, email: '[email protected]' },

        { name: null, email: '[email protected]' },

        { name: null, email: '[email protected]' }

      ]

    },

    {

      owner: 'owner2',

      collaborators: [

        { name: null, email: '[email protected]' },

        { name: null, email: '[email protected]' },

        { name: null, email: '[email protected]' },

        { name: null, email: '[email protected]' }

      ]

    },

    {

      owner: 'owner3',

      collaborators: [

        { name: null, email: '[email protected]' },

        { name: null, email: '[email protected]' },

        { name: null, email: '[email protected]' },

        { name: null, email: '[email protected]' }

      ]

    }

  ]

};


(async () => {

  

  await Promise.all(project.rows.map(async row => {

    

    row.owner = await serializeAndCache(row.owner);

    return Promise.all(row.collaborators.map(async collab => {

      

      let c = await serializeAndCache(collab.email);

      if (c) {

        collab.name = c.name;

        delete collab.role;

      }

      

    }));

    

  }));

  

})();

如您所見,許多 Promise 都同時處于待處理狀態(總體而言,代碼完成得更快)。您的優化似乎奏效了!我只能假設背后的任何邏輯serializeAndCache在同時被許多調用淹沒時表現不佳。這似乎是性能不佳的唯一解釋。



查看完整回答
反對 回復 2022-10-08
  • 1 回答
  • 0 關注
  • 221 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號