简体   繁体   English

javascript中的for循环内的异步函数调用

[英]Asynchronous function calls inside for loop in javascript

Writing a small Instagram like checking bot. 编写像检查机器人一样的小型Instagram。

How it works is, i have an object which is a list of users from a chat and their instagram usernames. 它是如何工作的,我有一个对象,它是聊天中的用户列表及其instagram用户名。

So i have this little piece of code here, it iterates over the list, gets the likers of the latest media post of each username and proceeds to check, all while in the loop. 所以我在这里有这小段代码,遍历列表,获取每个用户名最新媒体帖子的喜欢者,然后继续进行检查。 However the problem im facing is that there are asynchronous calls inside the loops, and the loop keeps iterating but the results of the async calls come later. 但是,面临的问题是循环中存在异步调用,并且循环不断进行迭代,但是异步调用的结果会稍后出现。 However, that is not my desired way of operation. 但是,这不是我想要的操作方式。 Could someone show me as to how i can overcome this, or the right way to do this in an asynchronous nature? 有人可以向我展示如何克服这个问题,或者以异步的方式来解决这个问题的正确方法吗? Thanks Below is my code, there is also a comment in there which shows you the format of the list object, just in case anyone is wondering. 谢谢下面是我的代码,在其中也有一条注释,向您显示了列表对象的格式,以防万一有人想知道。 Thanks 谢谢

bot.on('/check', (msg) => {
    list['12345'] = {username:'someguy', list:['@the.haider.ali']};
    list['45678'] = {username:'someguy2', list:['@vishnuvayala']};
    if(list.length != 0){
        for(i in list){
            var list2 = JSON.parse(JSON.stringify(list));
            delete list2[i];
            for(x in list2){
                for(y in list2[x].list){
                    let checkis = list2[x].list[y]
                    console.log("Checking "+checkis);
                    getInstaID(checkis).then(function(userid){
                        return getMediaID(userid);
                    }).then(function(mediaid){
                        return getLikers(mediaid);
                    }).then(function(likers){
                        console.log("Last liker is "+likers[0].username);
                    });
                    console.log("Done checking "+checkis);
                }
            }
        }
    }
});

Here's the output i get 这是我得到的输出

Checking @the.haider.ali
Done checking @the.haider.ali
Checking @vishnuvayala
Done checking @vishnuvayala
Last liker is ghetto._boy
Last liker is dk.gayathri

Try with async await. 尝试异步等待。 Async function can be blocked by await/promise, there for array iteration can be blocked until await response. 异步功能可以由await / promise阻止,那里的数组迭代可以被阻塞直到await响应。

Don't be afraid of Await expressions. 不要害怕Await表达式。 It'll just block function till same well known Promise won't be resolved. 它只会阻塞功能,直到不会解决同样著名的Promise。

Please check if it works. 请检查是否有效。

bot.on('/check', async function(msg){
    list['12345'] = {username:'someguy', list:['@the.haider.ali']};
    list['45678'] = {username:'someguy2', list:['@vishnuvayala']};
    if(list.length != 0){
        for(i in list){
            var list2 = JSON.parse(JSON.stringify(list));
            delete list2[i];
            for(x in list2){
                for(y in list2[x].list){
                    let checkis = list2[x].list[y]
                    console.log("Checking "+checkis);
                    var userid = await getInstaID(checkis);
                    var mediaid = await getMediaID(userid);
                    var likers = await getLikers(mediaid);
                    console.log("Last liker is "+likers[0].username);
                    console.log("Done checking "+checkis);
                }
            }
        }
    }
});

Output should be like 输出应该像

Checking 0
Last liker is likers: 0
Done checking 0
Checking 1
Last liker is likers: 1
Done checking 1
Checking 2
Last liker is likers: 2
Done checking 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM