Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

js for循环结束后的回调函数

let defaultData = []
      for (var i = 0; i < data.length; i++) {
        queryPerm({ appId: data[i].id, roleId: row.id }).then(res => {
          console.log('res.data', res.data)
          for (var i = 0; i < res.data.length; i++) {
            if (res.data[i]) {
              defaultData.push(res.data[i])
            }
          }
        })

      }
      console.log('defaultData', defaultData)

如题,我想知道这个for循环结束后,用什么方法可以得到他的回调函数,在最后一次循环结束后

在这里先感谢各位大婶们的解答了,小弟感激不尽!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

let defaultData = []
Promise.all(data.map(dataItem => queryPerm({ appId: data[i].id, roleId: row.id })))
.then(resList => {
    resList.forEach(res => {
         console.log('res.data', res.data);
         for (var i = 0; i < res.data.length; i++) {
            if (res.data[i]) {
              defaultData.push(res.data[i])
            }
         }
    })
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...