javascript - 如何在 for 循环中使用回调?- javascript
问题描述
我在从 for 循环内的回调中获取数据时遇到问题
getClipsFromFollowers: function (gameList, streamerList, callback) {
var allClips = []
console.log("gamelist in getclips", gameList);
for (game of gameList) {
console.log("game in getclips", game);
clipsGameManager.getListByName(game, function (error, list) {
if (list == []) {
twitchManager.getTopClipsFromGame(game, function (clips) {
clipsGameManager.createGameClips(game, clips, function (error, status) {
if (error != null) {
console.log(error)
} else {
if(list != null){
console.log("listfromGame getclips", list)
allClips = allClips.concat(list)
}
}
})
allClips = allClips.concat(clips)
console.log(clips);
})
}
})
}
console.log("allclips in getclips", allClips);
callback(null, allClips)
}
日志如下所示:
gamelist in getclips [ 'Overwatch' ]
game in getclips Overwatch
allclips in getclips []
[]
所以我的问题是我能做些什么来完成这项工作?我尝试使用 await() 没有成功。提前致谢!
解决方案
推荐阅读
- postgresql - 如何修复 pgAdmin4 连接被拒绝错误
- excel-formula - 自动根据相邻的卖出值增加单元格的值加上搜索最后一个数字递增 1
- robotics - 在 Fedora 上安装 MRPT
- java - 如何解决原始对象(如整数、字符串)不允许派生的限制?
- html - 为什么我在尝试获取我的 css 文件时收到错误 404?
- reactjs - 无法在 Mongoose 中使用 async/await 将游标方法应用于变量中的投影 ID 数组
- knitr - .Rnw 文件中的 knitr 章节参考书目?
- python - pandas dataframe groupby:应用涉及分组索引值的函数
- mysql - Docker 容器显示错误的磁盘大小
- r - 使用 rma.mv() 计算 R 中的整体效果大小:使用“子集”还是“mods”?