Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: Is there a better way to sync two promises?

$http.get('/services/menu').success(function (dataMenu) {
    $http.get('/services/language').success(function (dataLanguage) {
            console.log('dataLanguage', dataLanguage);
            $scope.menuRoot = dataMenu.result.items;
            $scope.menuItems = dataMenu.result.items;
            $scope.languageRoot = dataLanguage.result;
            $scope.currentMenuItem = null;
            $scope.refreshExperiments();
            $scope.mergeLanguage();
    });
});
like image 313
Guy Avatar asked Aug 05 '13 16:08

Guy


1 Answers

Yes, you can run them in parallel by using $q.all:

$q.all([$http.get('/services/menu'), $http.get('/services/language')])
   .then(function (res) {
        var dataMenu = res[0].data.result.items;
        console.log('dataLanguage', res[1].data);
        $scope.menuRoot = dataMenu;
        $scope.menuItems = dataMenu;
        $scope.languageRoot = res[1].data.result;
        $scope.currentMenuItem = null;
        $scope.refreshExperiments();
        $scope.mergeLanguage();
});
like image 184
Bergi Avatar answered Nov 06 '22 17:11

Bergi