简体   繁体   中英

Returning a custom promise only when needed - what am I doing incorrectly?

I am trying to write a function that returns a custom promise only when needed. The code looks like this:

login(sp: ServerProfile): Promise<any> {
    return this.logout(sp) // first logout
    .then(_ => {
        if (this.isAuthEnabled(sp)) {
          return this._login(sp) // if yes, re-login
          .then(succ => { this.utils.debug(`Logged in with: ${succ}`); this._isLoggedIn = true; })
        }
        else {
          this._isLoggedIn = true;
          return new Promise ( (resolve, reject ) => {resolve(true)}) //-> problem

        }

      })
  }

I know I can wrap the entire function in a new Promise (resolve, reject) but I don't want to unnecessarily wrap this._login as it returns a promise.

If I add the new Promise in the else{} part, I get this error:

[ts] Argument of type '(_: any) => Promise | Promise<{}>' is not assignable to parameter of type '(value: any) => void | PromiseLike'.

嗯,解决方法似乎是在手工制作的Promise中指定<any>

  return new Promise <any>( (resolve, reject ) => {resolve(true)}) //-> no problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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