最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

For循环顺序(带异步功能)

IT培训 admin 9浏览 0评论

For循环顺序(带异步功能)

我有这个代码:

exports.formatDepArrTables = function(jsonReturn, type, placeName, callback) {
    let toSend = "";

    if (type == 'departure') {
        for (let j = 0; j < jsonReturn.departures.length; j++) {
            console.log("DEPARTURES LOOP j = " + j + "/" + jsonReturn.departures.length);

            if(currentDeparture.display_informations.links[0] === undefined) {
                toSend += ""; // setting some string informations
            }
            else {
                let oceTrainId = ""; // random number given by a json
                _this.getDisruptionFromDisruptionId(oceTrainId, function(data) {
                    for(let i = 0; i < data.disruptions[0].impacted_objects[0].impacted_stops.length; i++) {
                        if(currentImpactedStop.stop_point.label == placeName) {
                            toSend += "string";
                        }
                    }
                });
            }
        }
        console.log("End of the first loop");
        return callback(toSend);

    } else if (type == 'arrival') {
        // copy&paste
    } else {
        throw new Error("not defined");
    }
};

当我运行这个代码(我使用NodeJS)时,它从第一个循环得到1 / 10,2 / 10 ......但它没有在第一个循环的第一次迭代中迭代第二个循环(它显示“结束”第一个循环“并开始第二个循环”。

getDisruptionFromDisruptionId是一个自定义方法,它在API上发出请求(带有'请求'NodeJS模块)。

当然,我需要有getDisruptionFromDisruptionId给出的信息来运行我的下一个循环......

此代码部分的父函数返回一个需要在两个循环结束时“填充”的回调。

有任何想法吗 ?

回答如下:

request是异步函数,您需要在代码中添加async / await或使用递归

For循环顺序(带异步功能)

我有这个代码:

exports.formatDepArrTables = function(jsonReturn, type, placeName, callback) {
    let toSend = "";

    if (type == 'departure') {
        for (let j = 0; j < jsonReturn.departures.length; j++) {
            console.log("DEPARTURES LOOP j = " + j + "/" + jsonReturn.departures.length);

            if(currentDeparture.display_informations.links[0] === undefined) {
                toSend += ""; // setting some string informations
            }
            else {
                let oceTrainId = ""; // random number given by a json
                _this.getDisruptionFromDisruptionId(oceTrainId, function(data) {
                    for(let i = 0; i < data.disruptions[0].impacted_objects[0].impacted_stops.length; i++) {
                        if(currentImpactedStop.stop_point.label == placeName) {
                            toSend += "string";
                        }
                    }
                });
            }
        }
        console.log("End of the first loop");
        return callback(toSend);

    } else if (type == 'arrival') {
        // copy&paste
    } else {
        throw new Error("not defined");
    }
};

当我运行这个代码(我使用NodeJS)时,它从第一个循环得到1 / 10,2 / 10 ......但它没有在第一个循环的第一次迭代中迭代第二个循环(它显示“结束”第一个循环“并开始第二个循环”。

getDisruptionFromDisruptionId是一个自定义方法,它在API上发出请求(带有'请求'NodeJS模块)。

当然,我需要有getDisruptionFromDisruptionId给出的信息来运行我的下一个循环......

此代码部分的父函数返回一个需要在两个循环结束时“填充”的回调。

有任何想法吗 ?

回答如下:

request是异步函数,您需要在代码中添加async / await或使用递归

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论