0

Suppose I have 2 or more async calls:

    $scope.obja = {};
    $scope.objb = {};

    ref1.on('value', function (value) {
        $scope.obja= value.val();
    });

    ref2.on('value', function (value) {
        $scope.objb= value.val();
    });

Later within the script I want to make sure that both of the $scope variables are set before I use them.

This example code follows the firebase examples in angularjs.

Any ideas?

theonlygusti
  • 11,032
  • 11
  • 64
  • 119
Patrioticcow
  • 26,422
  • 75
  • 217
  • 337

1 Answers1

0

Surely you can just check with an if statement?

if (($scope.obja != {}) && ($scope.objb != {})) {
    // they have been set
}

Or, if they stand a chance of getting set to an empty object, just have a second variable that gets set with them:

var setn = 0;

// ...

ref1.on('value', function (value) {
    $scope.obja= value.val();
    setn++;
});
ref2.on('value', function (value) {
    $scope.objb= value.val();
    setn++;
});

// ...

// later in code, check if they have both been set
if (setn == 2) {
    // they have both been set
}
theonlygusti
  • 11,032
  • 11
  • 64
  • 119