0

I have a function which is going to make a REST call, but it cannot do this until an auth token has been fetched.

So I wrapped the REST call in the 'then()' of the auth token call's promise, like so:

var RESTCall = function() {
  return authTokenPromise.then(function() {
    return $http.get('userService' {
      userId: 1234,
      someFlag: true
    });
  });
};

This has the result of waiting to fire off the call to the userService until the authToken promise (from the service that gets the auth token) has resolved.

The problem comes when I try to remove the hard-coded params that set userId and someFlag. What I want to do is this:

var RESTCall = function(params) {
  return authTokenPromise.then(function() {
    return $http.get('userService' {
      userId: params.userId, // params is undefined
      someFlag: params.flag   // params is undefined
    });
  });
};

How do I pass params into the anonymous function scope created by then(function() {...})?

tengen
  • 2,125
  • 3
  • 31
  • 57
  • you don't need to, just use params and it will work. aint closure grand? – dandavis Nov 13 '14 at 20:40
  • That should work. How and where are you calling `RESTCall`? Are you sure that in all occurences you actually pass an object? – Bergi Nov 13 '14 at 20:40
  • Use jquery function $.proxy http://stackoverflow.com/questions/3349380/jquery-proxy-usage – feesar Nov 13 '14 at 20:54
  • 1
    @dandavis @Bergi was right (took me down the right path)... I didn't need to do anything except that in my actual code (not my example here) also had a params like so: `authTokenPromise.then(params)`, except the authTokenPromise did not return anything, so the `params` in the RESTCall declaration was being overwritten (I assume) by the undefined `params` in the `.then(function(params) {}`. Thank you both... stupid typo... my bad. – tengen Nov 13 '14 at 21:13
  • @Bergi If you want to create an answer I'll accept it! – tengen Nov 13 '14 at 21:13

0 Answers0