首页 > 解决方案 > 如何在 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() 没有成功。提前致谢!

标签: javascriptnode.jsasynchronouscallbackasync-await

解决方案


推荐阅读