例如,我有四個功能:如何同步調用多個功能angularjs
var f1 = function() {...};
var f2 = function() {...};
var f3 = function() {...};
var f4 = function() {...};
var fmain = function() {...};
主要功能是一個循環:
var fmain = function() {
angular.forEach(question_list, function (question, key) {
f3(); //I want to execute f4() after f3() is returned!
f4();
});
};
在f3()
,f2()
被稱爲!
var f2() = function(){
//There's a timeout function to check if the dynamic value equals to the expected value
//if so, then return true; otherwise, keep calling f2() until the dynamic value equals to the expected value
}
在f2()
,f1()
被稱爲!
var f1() = function(){
//There's a timeout function to check if the dynamic value equals to the expected value
//if so, then return true; otherwise, keep calling f1() until the dynamic value equals to the expected value
}
所以,f3
取決於f2
,f2
取決於f1
。
我想讓它們同步返回(如果上一行沒有返回,需要代碼不要繼續下一行)。我怎樣才能實現這個?
在此先感謝!
除非有服務器(api)調用,否則上述所有功能都將同步執行。你能解釋一下你有沒有異步操作? – Aruna
使用承諾,角度中的$ q模塊應該有助於https://docs.angularjs.org/api/ng/service/$q – aliasav