Welcome to OGeek Q&A 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 Reply

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])
            }
         }
    })
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...