0

The function getIdentityTokenDecrypted is giving me a hard time. I'd like to return the promise from decryptToken SERVICE B to getToken SERVICE A, to retrieve the token.

Here are the steps I need :

  1. Get outlookService.mailbox.getUserIdentityTokenAsync result. (Gives an encrypted token)
  2. Decrypt token via $http route /api/exchange/createAndValidateIdentityToken. This request returns the token I need in SERVICE A.

How can I get this to work ?

/*** SERVICE A ***/

var service = {
    /*...*/
    token: getToken()
};

return service;

function getToken() {
    var token;

    serviceB.getIdentityTokenDecrypted()
        .then(function successCallback(response) {
            token = response.data.UniqueUserIdentification;
            return token;
        }, function errorCallback(response) {
            return null;
        });
}

/*** SERVICE B ***/

function getIdentityTokenDecrypted() {
    var token = null;
    var promise;

     // This async call does not return a promise,
     // I don't think I can chain after it.
     outlookService.mailbox.getUserIdentityTokenAsync(function (res) {
         token = res;
     });

     // That's why I use an interval
     promise = $interval(function () {
         if (token != null) {
             $interval.cancel(promise);
             return decryptToken();
         }
     }, 100);

     function decryptToken() {
         var location = window.location.href;

         // I need to get the 'data' from the success
         // of this request to retrieve the token
         return $http({
             method: "POST",
             url: "/api/exchange/createAndValidateIdentityToken",
             data: JSON.stringify({
                 userIdentityToken: token,
                 location: location
             })
         });
     };
     return promise;
};
1
  • There is the first parameter that is a callback method : msdn.microsoft.com/en-us/library/office/fp142236.aspx Though it leads to the same issue, I don't know how to return the promise of the $hhtp call from this callback function. Commented Oct 19, 2015 at 15:45

1 Answer 1

4

Since outlookService.mailbox.getUserIdentityTokenAsync provides a callback, you don't need $interval. You can promise-ify any async function that gives you a callback, by creating your own promise (this is fine when it's unavoidable) and then settling it with the promise you get from $http when you have it (which you won't until your callback for getUserIdentityTokenAsync is triggered).

I don't "do" Angular, but apparently $q has a "streamlined" syntax rather like ES2015's:

promise = $q(function(resolve, reject) {
    outlookService.mailbox.getUserIdentityTokenAsync(function (res) {
        if (/*...presumably there's some failure condition...*/) {
            reject(/*...*/);
        } else {
            token = res;
            resolve(decryptToken());
        }
    });
});

(I'd probably also modify decryptToken to accept the token as an argument rather than having the token variable.)

The key bit there is that if you resolve a promise with another promise, that will propagate through the chain.

Sign up to request clarification or add additional context in comments.

10 Comments

"Use callback, not $interval", you're right that made no sense.^^
@Elfayer: I'm embarrassed to say I didn't notice you were using the callback until I'd already written most of the answer, which opened with that same paragraph followed by "But that doesn't change the fact that you have to somehow get a promise out of a callback situation" and then showed the above, but with the interval. Fortunately I caught it in time. :-) But now I've just removed the first paragraph entirely, it didn't serve any purpose.
Since this is AngularJS OP's talking about, it would be better to go with the $q service instead. It's pretty much the same syntax, but wrapped in a $watch
@Guilherme I looked into it, I don't really get the use to create a promise manually since the $http returns one.
@Guilherme: Thanks. I'd be guessing. If you want to post an answer copying the above with with the correct syntax, I'd be happy to delete this one in favor of yours. Or I think you can suggest an edit even though you can't directly edit.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.