Search code examples
javascriptpromiseswaggerbluebird

Bluebird Promise with Swagger: return is not a function


I'm using Swagger to create my JS API client for calling a set of REST endpoints. I'm configuring the Swagger client to use Promises (new Swagger({ spec: spec, usePromise: true })).

I require Bluebird before Swagger, to use Bluebird promises. But my app is blowing up when trying to use Promise.return.

The code looks like

client.user.get_db_user(db).return("foo");

where client is the Swagger reference.

The error I get is

app: TypeError: client.user.get_db_user(...).return is not a function

It works if I replace return with then(() => value). return is supposed to be shorthand for this sort of use of then.

After investigating, I see it's failing on promises returned by the Swagger client.

What's happening?


Solution

  • It turns out that Swagger uses the Q promise library explicitly under the hood.

    Bluebird and Q are partially compatible, so many things work fine. The problem happens when trying to use parts of the Bluebird API that Q doesn't have.

    In particular, Bluebird has "return", whereas Q does not.

    The easiest answer is to use the then construction discussed in the question.

    Other possible solutions I have yet to try:

    1. Use the "bluebird-q" project to replace Q with Bluebird.
    2. promisifyAll Swagger instead of using Swagger's built-in promise support.