-1
私はこのコードを持っている: (非同期機能付)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を使用)このコードを実行すると、それは10分の1になり、2月10日...からの最初のループの最初の反復で2番目のループを繰り返しませんでした(「最初のループの終わり」を示し、2番目のループを開始します)。
getDisruptionFromDisruptionId
は、APIに対してリクエスト(NodeJSモジュールの 'request'を指定)を行うカスタムメソッドです。
はもちろん、私はこのコードの一部の
親関数は、両方の末尾に「埋め」する必要があり、コールバックを返して...私の次のループを実行するためにgetDisruptionFromDisruptionId
によって与えられた情報を持っている必要があります2つのループ。
アイデア?
request'は非同期関数である '、あなたが/非同期を追加する必要が/非同期の追加あなたのコードや使用再帰 – Peter
に待っています今のところ魅力的な仕事を待っています!ありがとう! – OverSpeed301