2016-09-19 6 views
1

私は、多くの残りの呼び出しを行うとPairTypeValuesオブジェクトに応答を保存しています次のJavaScriptコード前のブロックが終了した後にJavaScriptブロックが実行されるようにするにはどうすればよいですか?

function getWorkflowSchemeName(projectKey, callback){ 
 
\t var restCall = AJS.params.baseURL+"/rest/projectconfig/1/workflowscheme/"+projectKey 
 

 
\t AJS.$.get(restCall, function(response){ 
 
\t \t if(response != null){ 
 
\t \t \t callback(response.name) 
 
\t \t } 
 
\t \t console.log("Im in here") 
 
\t }) 
 
} 
 

 
pairTypeValues = {} 
 
AJS.$.each(AJS.$(".projects-list tr td:nth-child(3)"), function(index, value){ 
 
\t 
 
\t getWorkflowSchemeName(value.innerText, function(workflowSchemeName){ 
 
\t \t pairTypeValues[value.innerText] = workflowSchemeName 
 
\t \t }) 
 
}) 
 

 
//The following code MUST run after ALL the pairTypeValues are recieved. 
 

 
counter = 1 \t 
 
AJS.$.each(AJS.$(".projects-list tr td:nth-child(3)"), function(ind,val){ 
 
\t AJS.$.each(pairTypeValues, function(index,value){ 
 
\t \t if(val.innerText == index){ 
 
\t \t \t console.log("SUP") 
 
\t \t \t AJS.$(".projects-list tr:nth-child("+counter+")").append("<td>"+value+"</td>") \t 
 
\t \t } 
 
\t }) 
 
\t counter++ 
 
})

を持っています。 (すべてのデータを取得するのに時間がかかります)

最後のコードブロックは、PairTypeValuesで見つかったデータを追加します。

私はすべての値がPairTypeValuesオブジェクトに格納されるまで、(単一ファイルの実行ではなく)最後のブロックを別々に実行しようとしましたが、正常に動作します。しかし、コードをまとめて実行すると、何も印刷されません。

私は別のコールバックを追加することでそれをやってみましたが、それはうまくいきませんでした:

function getWorkflowSchemeName(projectKey, callback){ 
 
\t var restCall = AJS.params.baseURL+"/rest/projectconfig/1/workflowscheme/"+projectKey 
 

 
\t AJS.$.get(restCall, function(response){ 
 
\t \t if(response != null){ 
 
\t \t \t callback(response.name) 
 
\t \t } 
 
\t \t console.log("Im in here") 
 
\t }) 
 
} 
 

 
function makingPairTypes(anotherCallback){ 
 
\t pairTypeValues = {} 
 
\t AJS.$.each(AJS.$(".projects-list tr td:nth-child(3)"), function(index, value){ 
 
\t 
 
\t \t getWorkflowSchemeName(value.innerText, function(workflowSchemeName){ 
 
\t \t \t anotherCallback(pairTypeValues[value.innerText] = workflowSchemeName) 
 
\t \t \t }) 
 
\t }) 
 
} 
 

 
\t counter = 1 \t 
 
\t AJS.$.each(AJS.$(".projects-list tr td:nth-child(3)"), function(ind,val){ 
 
\t \t makingPairTypes(function(secondCallback){ 
 
\t \t AJS.$.each(secondCallback, function(index,value){ 
 
\t \t \t if(val.innerText == index){ 
 
\t \t \t \t console.log("SUP") 
 
\t \t \t \t AJS.$(".projects-list tr:nth-child("+counter+")").append("<td>"+value+"</td>") \t 
 
\t \t \t } 
 
\t \t }) 
 
\t \t }) 
 
\t \t counter++ 
 
\t })

は、私はまた、繰延メソッドを使用してみましたが、それは私のために働いていません次のいずれか

function makingPairTypes(){ 
 
var pairTypeValues = {} 
 
AJS.$.each(AJS.$(".projects-list tr td:nth-child(3)"), function(index, value){ 
 
\t 
 
\t getWorkflowSchemeName(value.innerText, function(workflowSchemeName){ 
 
\t \t pairTypeValues[value.innerText] = workflowSchemeName 
 
\t \t }) 
 
}) 
 
} 
 

 
//The following code MUST run after ALL the pairTypeValues are recieved. 
 

 
function addingSchemeNames(){ 
 
counter = 1 \t 
 
AJS.$.each(AJS.$(".projects-list tr td:nth-child(3)"), function(ind,val){ 
 
\t AJS.$.each(pairTypeValues, function(index,value){ 
 
\t \t if(val.innerText == index){ 
 
\t \t \t console.log("SUP") 
 
\t \t \t AJS.$(".projects-list tr:nth-child("+counter+")").append("<td>"+value+"</td>") \t 
 
\t \t } 
 
\t }) 
 
\t counter++ 
 
}) 
 
} 
 

 
var dm = AJS.$.Deferred(); 
 

 
dm.done([makingPairTypes, addingSchemeNames]);

最後のブロックが実行される前にすべてのpairTypeValuesが収集されていることを確認したいだけです。

誰かお手伝いできますか? コードにSetTimeOutsを挿入したくありません。

多くのおかげで、事前に

+1

使用[生命維持(https://en.wikipedia.org/wiki/Immediately-invoked_function_expression)および[約束](https://developer.mozilla.org/en/docs/Web/JavaScript/参照/ Global_Objects/Promise) –

+0

iifeも動作しませんでした。約束のために 、私がしようとしている: するvar F1 =新しい約束(funciton(履行を、拒否){ 第二のブロック の//コード}) 、その後 f1.then(*最後のブロック*のコード) これも機能しませんでしたか? 私はそれを正しくしていますか? –

+0

あなたは約束をどこで解決していますか? –

答えて

1

は約束とPromise.allを使用してのような近代的な解決策があります。しかし、私はあなたが持っているものは、実際には働いているロジックのほんの一片であると思います。実行中のレスポンスの数を追跡し、レスポンスコールバックが実行されるたびに更新し、レスポンスコールバック内から最終コード関数を呼び出すだけで済みます。

function getWorkflowSchemeName(projectKey, callback){ 
    var restCall = AJS.params.baseURL+"/rest/projectconfig/1/workflowscheme/"+projectKey; 

    AJS.$.get(restCall, function(response){ 
     callback(response); 
    }) 
}; 

pairTypeValues = {}; 
doneIfZero = AJS.$(".projects-list tr td:nth-child(3)").length; 
AJS.$.each(AJS.$(".projects-list tr td:nth-child(3)"), function(index, value){ 
    getWorkflowSchemeName(value.innerText, function(response){ 
     var workflowSchemeName = response.name; 
     if(workflowSchemeName != null){ 
      pairTypeValues[value.innerText] = workflowSchemeName; 
     } 
     doneIfZero--; 
     if (doneIfZero === 0) { 
      codeToRunAfterAllResponsesAreBack() 
     } 
    }); 
}); 

//The following code MUST run after ALL the pairTypeValues are recieved. 
function codeToRunAfterAllResponsesAreBack(){ 
    counter = 1 
    AJS.$.each(AJS.$(".projects-list tr td:nth-child(3)"), function(ind,val){ 
     AJS.$.each(pairTypeValues, function(index,value){ 
      if(val.innerText == index){ 
       AJS.$(".projects-list tr:nth-child("+counter+")").append("<td>"+value+"</td>") 
      } 
     }); 
     counter++ 
    }); 
}; 
+0

注:外側のループから 'ind'の代わりにcounterを使用する理由はわかりませんが、非同期の実行順序の問題に影響していない「そのままの状態」のコード。 –

+0

これはまさに私がやりたかったことです。ありがとうございました。 –

関連する問題