Hy L
Hy L

Reputation: 582

How to synchronously call multiple functions angularjs

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!

Upvotes: 0

Views: 2934

Answers (2)

Saurabh Jain
Saurabh Jain

Reputation: 11

        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.

Upvotes: 0

strelok2010
strelok2010

Reputation: 1266

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();
  });
});
};

Upvotes: 1

Related Questions