繁体   English   中英

承诺在解决之前不等待嵌套承诺

[英]Promise is not waiting for nest promises before resolving

我有这个初始化函数,用于为我的应用程序设置全局依赖项我正在使用npm软件包angular-global-resolve 该包工作正常,问题是我在初始化函数中有一个很大的承诺,其中包含嵌套的promises,并且main promise在嵌套之前解析,这意味着在应用程序第一次运行时没有设置某些东西。 我该如何解决?

我的代码:
在routes.js中:

  globalResolveProvider.addGlobalDependenciesTo($stateProvider, {
    getGlobalDependacies: function ($http, $rootScope, $cookies) {
      return $http.get('/__/env.json')
      .then(function(response) {
        console.log('then0')
        $rootScope.apiUrl = response.data.apiUrl;
        $rootScope.googleMapsApiKey = response.data.googleMapsApiKey;
        $rootScope.currentLocationLat = 40.7589;
        $rootScope.currentLocationLng = 73.9851;
        var hotelId = ''
        if ($cookies.get('hotel') === undefined){
          console.log('if 1')
          $http.get($rootScope.apiUrl + '/hotels')
          .then(function(dbHotels){
            console.log('then1')
            hotelId = dbHotels.data[0]._id
            $cookies.put('hotelId', hotelId)
          })
        }
        if ($cookies.get('userId') === undefined){
          console.log('if 2')
          $http.get($rootScope.apiUrl + '/users')
          .then(function(dbUsers){
            console.log('then2')
            var index = dbUsers.data.length - 1
            var userId = dbUsers.data[index]._id
            $cookies.put('userId', userId)
            $rootScope.$broadcast('update-itinerary-icon')
          })
        }
      })
      .then(function(){
        console.log("parent then is resolved")
      })
    }
  })

控制台正在记录:

then0
if 1
if 2
parent then is resolved
then1
then2

为什么家长则之前解决then1then2

当你这样做

$http.get($rootScope.apiUrl + '/hotels')

要么:

$http.get($rootScope.apiUrl + '/users')

这仅开始 HTTP请求。 流继续在父承诺中,而不等待嵌套的GET完成。 然后父母像往常一样解决。

如果要让父级等待子级承诺,则必须使父级返回一个承诺

return $http.get($rootScope.apiUrl + '/hotels')

现在,如果您想让父级等待多个子级承诺,您必须返回一个组成多个promise的promise $q注入到您的函数中,并将其用作return语句:

return $q.all(
    $http.get($rootScope.apiUrl + '/hotels'),
    $http.get($rootScope.apiUrl + '/users')
);

如果你想要条件逻辑,那么你可以尝试这样的事情:

return $q.all(
    ($cookies.get('hotel') === undefined)
    ? $http.get($rootScope.apiUrl + '/hotels')
    : $q.resolve(),
    ($cookies.get('userId') === undefined)
    ? $http.get($rootScope.apiUrl + '/users')
    : $q.resolve()
);

这使得它等待已经完成的承诺,在不需要工作的情况下。

您还可以添加你的.then()$http.get()在上面的例子。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM