Search code examples
angularjshttpget

AngularJS - Share return values between promises


I'm making a get request to my server, I get the response and I store the value inside a $scope.productId

  userService.get(true)
    .then(function(res) {
      $scope.productId = res.user.productid;
      }
    });

then I need to use this value in another get request to the api to get the product related to this id.

  apiService.get('/product/' + ???)

  .then(function(response) {
    console.log(response)
  })

  .catch(function(response) {
  });

I'm new to promises, so the objective is to get the value of the first request in to the second one!


Solution

  • use this

    userService.get(true)
        .then(function(res) {
          $scope.productId = res.user.productid;
          apiService.get('/product/' + ???)
    
          .then(function(response) {
            console.log(response)
          })
    
          .catch(function(response) {
          });
        }
      });