kentor
kentor

Reputation: 18524

Bluebird and es6 promises in typescript

I started a new project where I'd like to use TypeScript instead of pure Javascript. I am struggling with the use of Bluebird in conjunction with third party libraries.

See the following example:

  import * as Promise from 'bluebird'

  private requestPlayerProfile(playerTag:string):Promise<IPlayerProfile> {
    const requestOptions = Object.create(this.options)    
    return this.limiter.schedule(request, requestOptions)
  }

The problem: limiter is an instance of a third party library and limiter.schedule returns a native promise apparently, while I am using the Bluebird promises in the rest of my application. How would I properly handle such cases?

[ts] Type 'Promise' is not assignable to type 'Bluebird'. Types of property 'then' are incompatible.

Upvotes: 4

Views: 3386

Answers (1)

Igor Soloydenko
Igor Soloydenko

Reputation: 11825

@Filipe is interpreting the error message correctly.

  • Whatever is the type of object returned by this.limiter.schedule(...), that type is incompatible with bluebird.Promise<IPlayerProfile>.
  • Nobody can reliably assume that limiter.schedule(...) returns a "vanila", i.e. a native, Promise<IPlayerProfile> object. You can easily figure it out by going to the source code where schedule(...) is defined. In my Visual Studio code I use F12 to get there. Notice the precise return type of the object there.
  • Depending on what exactly is returned, you have two major options:
    1. Use that promise type everywhere in your code instead of relying on bluebird's promises altogether; or
    2. Convert that promise type to a bluebird one. I have not tried myself, but the following should work: return Promise.resolve(this.limiter.schedule(request, requestOptions)) (see http://bluebirdjs.com/docs/api/promise.resolve.html).

I hope it helps.

Upvotes: 3

Related Questions