2016-09-13 9 views
0

以下のコードを実行しようとしていますが、期待される出力が得られません。このノードスクリプトをデバッグする方法は?

recursive(prodURL, function(err, files) { 
    targetDeviceUpdate("updatedPath"); 
}); 
function targetDeviceUpdate(sourceImages, updatedPath) { 
    console.log("1 " + updatedPath); 
    recursive(prodURL + "/device", function(err, files) { 
     console.log("3 " + updatedPath); 
    }); 
} 

期待出力:

1 updatedPath

3 updatedPath

1 updatedPath

3 updatedPath

実際の出力:

1 updatedPath

1 updatedPath

3 updatedPath

3 updatedPath

+1

「再帰的」関数を提供できますか? – Nazar

答えて

0

さて、あなたはどのような機能をしたいですか?あなたのコードは、2番目の行から関数[自身]をもう一度呼び出すため、あなたの言うとおりに機能します。それから、最後に残ります。

織り交ぜる場合は、関数内で逐次呼び出す必要があります。再帰的に呼び出さないでください。再帰的な動作がテールアップされ、最終的なスパイラル化によって、この複製が作成されます。

CamelCaseはいいですが、javascriptはハイフンをマイナス記号以外のものとして解釈しないので、ハイパーズネーミング変数を使用することをお勧めします。アンダースコア_to_name_your_variables_cleanly 。

the_method_thatinvokes_target_device_update(prodURL, function(err, files) { 
    target_device_update("updatedPath"); 
}); 

function target_device_update(sourceImages, updated_path) { 
    console.log("Here is the first line: " + updated_path); 
    the_method_that_invokes_target_device_update(prodURL + "/device", function(err, files) { 
    //invoking this function will naturally call the first line again before reaching the rest of the code. 

    //and this 'callback' or 'remainder' code gets called only after, and twice at that. 
    console.log("3 " + updated_path); 

    }); 
} 

効果を得るには、再帰は必要ありません。変更を順番に呼び出すだけです。あなたは具体的に何を達成しようとしていますか?

+0

両方の再帰関数には異なる用途があります。私は毎回1500以上のファイルを使用しています。 – golekunal88

+0

@ golekunal88異なる機能で異なる作業をする – sova

関連する問題