Node.js 如何使用嵌套的foreach循环等待Async/wait完成

Node.js 如何使用嵌套的foreach循环等待Async/wait完成,node.js,async-await,Node.js,Async Await,如何等待getVideoCountForAuthor()完成所有异步查询 router.get('/', function (req, res, next) { console.log("START"); (async () => { let uniqueAuthors = await getInfluencersForHashTags("winter", { sessionList: [TTSessions]}

如何等待getVideoCountForAuthor()完成所有异步查询

router.get('/', function (req, res, next) {

    console.log("START");

    (async () => {
        let uniqueAuthors = await getInfluencersForHashTags("winter", {  sessionList: [TTSessions]});
        uniqueAuthors = await getVideoCountForAuthor(uniqueAuthors);
    })()
        .then(() => {
            console.log("EVERYTHING DONE");
        })  ;


});

async function getVideoCountForAuthor(authors) {

    return _.each(authors, async function (author, i, authors) {

        let feed = await TikTokScraper.user(author.username, {number: 10, sessionList: [TTSessions]});

        let authorMeta = author.authorMeta;

        let videocounts = _.map(feed.collector, function (post) {
            return post.playCount;
        });

        console.log("set videocount");

    });
}
日志:

START
EVERYTHING DONE
set videocount
set videocount
set videocount
set videocount

哦,哇。它与for循环配合使用。这是最好的选择吗

async function getVideoCountForAuthor(authors) {

    for (const author of authors) {

        let feed = await TikTokScraper.user(author.username, {number: 10, sessionList: [TTSessions]});

        let authorMeta = author.authorMeta;

        let videocounts = _.map(feed.collector, function (post) {
            return post.playCount;
        });

        //let newAuthor = author;
        // uniqueAuthors[i].medianVideoCounts = median(videocounts);
        console.log("set videocount");

    };
}

如果您想使用
lodash
,您可以使用
map
而不是
each
,因为map将返回一系列答案(在您的情况下为空承诺),因此要将该场景更改为每个场景的相同场景,您可以执行以下操作:

async function getVideoCountForAuthor(authors) {

    return _.map(authors, async function (author, i, authors) {

        let feed = await TikTokScraper.user(author.username, {number: 10, sessionList: [TTSessions]});

        let authorMeta = author.authorMeta;

        let videocounts = _.map(feed.collector, function (post) {
            return post.playCount;
        });

        console.log("set videocount");
        return author;
    });
}
现在,答案将是一系列承诺,因此您可以在
getVideoCountForAuthor
或父函数中创建一个承诺

第一种情况是:


async function getVideoCountForAuthor(authors) {

    return await Promise.all(_.map(authors, async function (author, i, authors) {

        let feed = await TikTokScraper.user(author.username, {number: 10, sessionList: [TTSessions]});

        let authorMeta = author.authorMeta;

        let videocounts = _.map(feed.collector, function (post) {
            return post.playCount;
        });

        console.log("set videocount");
        return author;
    }));
}
您的输出将是:

START
set videocount
set videocount
set videocount
set videocount
EVERYTHING DONE

视情况而定,我更喜欢一系列承诺,并使用
wait Promise.All(arrayOfPromises)
等待这一承诺。在的
中,当前一个作者完成时,将执行下一个作者。在一系列承诺中,作者将异步执行。因此,这取决于您和解决方案o:注意:在原始代码中,您返回的是
作者的数组。在此代码中,您将返回
未定义的
谢谢。lodash是否有一个“现代”的替代方案来在nodejs中获得所有这些帮助函数?我不使用
lodash
,所以我不能肯定地告诉您。但就其本身而言,这是javascript中的一个典型案例。有一个承诺的“forEach”是非常复杂的。我建议使用循环、映射或将它们存储在数组中,以便以后可以使用
Promise.all
wait
。我应该使用这3个选项中的哪一个?这取决于你的解决方案和你遇到的问题