ノードバックエンドでWebhookとソケットを処理するGithub Commit History Flow VisualizerをAngularで実装しています。私は約束を処理するためにノードパッケージdeferredを使用していますが、私は関数コールバック内で複雑な変数を割り当てるのに問題があります。私は約束が大好きですが、これは私に悩まされています。以下のコードを参照してください:非同期コールバック関数のノードオブジェクトの割り当てが無期限に掛かります
function getGithubRepoData(repoName) {
var d = deferred();
var reponseData = {
branches: { values: [] },
tags: { values: [] },
commits: [{ values: [] }]
}
deferred(getGithubRepoBranches(repoName), getGithubRepoTags(repoName))(function(result) {
console.log("both done");
console.log(result[0]);
console.log("blocking?");
responseData.branches.values = result[0];
console.log("past here?");
responseData.tags.values = result[1];
console.log("now do commits");
getGithubRepoCommits(repoName, result[0]).then(function(result) {
responseData.commits[0].values = result;
d.resolve(responseData);
})
})
return d.promise();
}
この機能は、ブランチ、タグ、およびコミット(すべてのブランチ)を取得するためのものです。問題は、responseData.branches.values
を割り当てようとするときに発生します。コードは次のように出力し、その後、ちょうどハングするまで適切に実行されます。
both done
4
blocking?
そして何も...
ここにあなたが私の問題を説明するために実行することができますコンセプトのシンプルな証拠です:
var deferred = require('deferred');
function dtest() {
var d = deferred();
setTimeout(function() {
d.resolve("good");
}, 500);
return d.promise;
}
var reponseData = {
branches: { values: [] },
tags: { values: [] },
commits: [{ values: [] }]
}
var tester = "test";
deferred(dtest())(function(result) {
tester = result;
console.log(result);
console.log("here?");
responseData.branches.values = result;
console.log("here?");
console.log(responseData.branches.values);
})
出力:
good
here?
をあなたが見ることができるように、単純な文字列変数はウィットに割り当てられていますhは問題ありませんが、より複雑なオブジェクトは無期限にハングします。
私が使用しているパッケージには私が見ていないエラーがありますが、私よりも多くのノード知識が欲しいと願っています。ロック問題または競合状態。これが機能しない場合、私は各関数をパラレルではなく順番に実行してみる必要があります。助けを前にありがとう!
スタンドアロンの例では、常に「良好」を解決します。この約束は、おそらくresponseData.branches.valuesが定義されていない場合にエラーを飲み込むことです。 編集:エラーの捕捉/処理については、「終了チェーン」セクションを参照してください。 https://github.com/medikoo/deferred/blob/master/README.md –
[繰延反パターン](http://stackoverflow.com/q/23803743/1048572)を避けてください! – Bergi