节点 - 等待循环完成? [英] Node - Wait for loop to finish?

查看:50
本文介绍了节点 - 等待循环完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当下面的函数完成并提供数组'albums'中的项目的最终列表时,我希望它调用另一个函数/对列表执行其他操作。

When the function below finishes and provides a finalized list of items in the array 'albums', I want it to call another function/do something else with the list.

目前它在函数完成之前发布[]并且我知道这是因为异步执行,但我认为Node是线性读取的,因为它是单线程的?

Currently it posts [] before the function finishes and I know that's because of asynchronous execution, but I thought Node read linearly since it's single threaded?

function getAlbumsTotal(list, params){
    for(var i = 0; i<list.length; i++){
        api.getArtistAlbums(list[i], params).then(function(data) {
            for(var alb = 0; alb<data.body.items.length; alb++){
                albums.push(data.body.items[alb].id);
            }
        }, function(err) {
            console.error(err);
        });
    }
    console.log(albums);
    //do something with the finalized list of albums here
}


推荐答案

您提供给然后的回调函数确实是异步执行的,这意味着它只在当前调用中的其余代码之后执行堆栈已经完成执行,包括你的最终 console.log

The callback function you provide to then is indeed executed asynchronously, which means it is only executed after the rest of the code in the current call stack has finished executing, including your final console.log.

这是你如何做到的:

function getAlbumsTotal(list, params){
    var promises = list.map(function (item) { // return array of promises
        // return the promise:
        return api.getArtistAlbums(item, params)
            .then(function(data) {
                for(var alb = 0; alb<data.body.items.length; alb++){
                    albums.push(data.body.items[alb].id);
                }
            }, function(err) {
                console.error(err);
            });
    });
    Promise.all(promises).then(function () {
        console.log(albums);
        //do something with the finalized list of albums here
    });
}

注意:显然专辑被定义为全局变量。这不是一个好设计。每个承诺提供自己的专辑子集会更好,并且 Promise.all 调用将用于将这些结果连接到局部变量。下面是这样的:

NB: Apparently albums is defined as a global variable. This is not so good a design. It would be better that each promise would provide its own subset of albums, and the Promise.all call would be used to concatenate those results into a local variable. Here is how that would look like:

function getAlbumsTotal(list, params){
    var promises = list.map(function (item) { // return array of promises
        // return the promise:
        return api.getArtistAlbums(item, params)
            .then(function(data) {
                // return the array of album IDs:
                return Array.from(data.body.items, function (alb) {
                    return alb.id;
                });
            }, function(err) {
                console.error(err);
            });
    });
    Promise.all(promises).then(function (albums) { // albums is 2D array
        albums = [].concat.apply([], albums); // flatten the array
        console.log(albums);
        //do something with the finalized list of albums here
    });
}

这篇关于节点 - 等待循环完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆