0

For example, I have four functions:

var f1 = function() {...};
var f2 = function() {...};
var f3 = function() {...};
var f4 = function() {...};
var fmain = function() {...};

The main function is a for loop:

  var fmain = function () {
    angular.forEach(question_list, function (question, key) {

      f3();  //I want to execute f4() after f3() is returned!

      f4();

    });

  };

In f3(), f2() is called!

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
}

In f2(), f1() is called!

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
}

So, f3 depends on f2, f2 depends on f1.

I want to have them return synchronously (Need the code not to proceed to next line if the previous line is not returned yet). How can I implement this?

Thanks in advance!

2
  • Unless there is server (api) calls, all the functions above will be executed synchronously. Can you explain do you have any asynchronous operation there? Commented Nov 10, 2016 at 8:32
  • Use promises, the $q module in angular should help docs.angularjs.org/api/ng/service/$q Commented Nov 10, 2016 at 8:48

2 Answers 2

1

You can use $q service:

var f1() = function(){
    var defer = $q.defer();
    $timeout(function(){
        defer.resolve(f1result);
    });
    return defer.promise;
}

var f2() = function(){
   var defer = $q.defer();
   f1().then(function(f1result){
         defer.resolve(f2result);
   });
   return defer.promise;
}

f3 function would work like f1 and f2 (defer,promise and resolve).

var fmain = function () {
angular.forEach(question_list, function (question, key) {
  f3().then(function(f3result){
      f4();
  });
});
};
Sign up to request clarification or add additional context in comments.

Comments

0
        return A()
            .then(function (response) {
                //this portion of code in then of A() (let's call it function B) will execute only after A() provides a response or is resolved.
                if (response.isAllowed == true) {
                    otherData = myFactory.getOtherDataFromServiceOne();
                }
                else {
                    otherData = hisFactory.getOtherDataFromServiceTwo();
                }
                return $q.all([
                    otherData
                ]).then(function (results) {
                    return {
                        otherData: results[0]
                    };
                });
            });
    }


    function A() {
            var isAllowed = myFactory.isAllowed(userId);
            return $q.all([
                isAllowed 
            ]).then(function (results) {
                return {
                    isAllowed : results[0].data;
                };
            });
    };

I would mention here that $q.all is only being used to represent that we can pass as many functions in each of one $q.all used here otherwise you can simply use a $promise.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.