例如,我有四个功能:如何同步调用多个功能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