2

I ended up having a promise like this in my code. Is it ok if I resolve in some condition with value of token which is a string (resolve(token)) and in one other condition resolve with another promise of type Promise<string> : resolve(resultPromise);

const finalPromise = new Promise<string>(resolve => {
    resultPromise.then(token => {
        if (Pending) {      
            resolve(token);
        } else if (!this.tokenIsValid(token)) {
            resultPromise = requestToken();
            resolve(resultPromise);
        } else {
            resolve(token);
        }
    });

I also tried changing it like this and got this typescript error:

//throwing error error noUnnecessaryCallbackWrapper: No need to wrap 'resolve' in another function. Just use it directly.

#second verison
    const finalPromise = new Promise<string>(resolve => {
    resultPromise.then(token => {
        if (Pending) {      
            resolve(token);
        } else if (!this.tokenIsValid(token)) {
            requestToken().then(token => resolve(token)); 
        } else {
            resolve(token);
        }
    });

What would be the type of finalPromise in case I am returning resolve(resultPromise)? what I am concerned is there is another function which gets the finalPromise and the type of the input is Promise<string> and I am worried that I am messing up responses and I am not aware. How should I rewrite this so that finalPromise returns Promise of type string? Thanks a lot for your help

AlreadyLost
  • 767
  • 2
  • 13
  • 28
  • would it be ... *any*? – Jaromanda X May 17 '18 at 02:38
  • what will happen if there is another function waiting to get the finalPromise as type of Promise? – AlreadyLost May 17 '18 at 02:41
  • sorry, I don't know typescript very well, just seen `` bandied about - guess you could try - thing is, your code is guilty of the [Promise constructor anti-pattern](https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it) so it's annoying to read :p – Jaromanda X May 17 '18 at 02:44

1 Answers1

1

As it was pointed in comments you are using Promise constructor anti-pattern, to avoid this you have to chain your code to resultPromise directly.

Moreover you can simplify the management of requestToken call by returning it:

Normally, a value returned by a .then handler is immediately passed to the next handler. But there’s an exception.

If the returned value is a promise, then the further execution is suspended until it settles. After that, the result of that promise is given to the next .then handler.

(Quoted from this article)

So you will end up with something like this:

const finalPromise: Promise<string> = resultPromise.then(token => {
    if (Pending) {      
        return token;
    } else if (!this.tokenIsValid(token)) {
        return requestToken();
    } else {
        return token;
    }
);

// Later 
finalPromise.then(stringToken => console.log(stringToken));

if-else statement can be simplify too but as I didn't know the type of Pending I didn't do it.


FYI: It was not your case here but if you really need someday to have multiple return types DO NOT USE any but use union type instead:

myAwesomeFunctionReturnStringOrNumber(): string | number {
   ...
};

myOtherFunctionTakeStringOrNumber(param: string | number) {
   ...
};
Community
  • 1
  • 1
Gaëtan Maisse
  • 12,208
  • 9
  • 44
  • 47