4

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.

kentor
  • 16,553
  • 20
  • 86
  • 144
  • 1
    It depends on the type of Promise the library you're using returns. If it returns a vanilla Promise, you could either map manually from one to the other or simply use the native Promises instead of bluebirds. – fs_ Oct 07 '17 at 23:37
  • What is a vanilla Promise? I think it returns a native ES6 promise and when I use the native Promise I would literally need to use native Promises everywhere or? So all routes / functions which call the requestPlayerProfile function need to work with Bluebird too. Doesn't really convince me to use Bluebird if I really need to mix native and bluebird Promises all the time? – kentor Oct 07 '17 at 23:50

1 Answers1

3

@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.

Igor Soloydenko
  • 11,067
  • 11
  • 47
  • 90