Bluebird: Confusing warning: a promise was created in a handler but was not returned from it

Created on 24 May 2017  Â·  9Comments  Â·  Source: petkaantonov/bluebird

1) What version of bluebird is the issue happening on? - 3.4.7
2) What platform and version? – Chrome, Firefox, Safari (require('bluebird') + browserify)
3) Did this issue happen with earlier version of bluebird? - no, 2.9*

Should not .done() to suppress the warning?

// some button.js
function onClick() {
    this.loadOptions()
        .then((options) => {
            // some external event interface
            this.trigger('loaded', options); // sync
        })
        .done();
}

// some external button listener like table.js
button.on('loaded', (e, options) => {
    this.loadSomeData(options)
        .then((data) => {
            this.updateSelf(data); // sync
        })
            .done();
});

We have warning bacause promise from loadSomeData() created in a handler of promise from loadOptions()

Also I think this warning works strange, two examples:

function test1() {
    Bluebird
        .resolve()
        .done(() => {
            Bluebird.resolve().done();
        });
}

no warning above

function test2() {
    Bluebird
        .resolve()
        .then(() => {
            Bluebird.resolve().done();
        })
        .done();
}

warning happened

Guess the examples are equal, but warning happening

I think that in both cases, the warning shouldn't be happen, because it is not possible to return promise in any case.

PS: all the examples are full synthetic but reflecting real cases

PPS: http://bluebirdjs.com/docs/api/promise.config.html#promise.config it says that all the options are false by default, but I get the warnings while not explicitly set {warnings: false}

Most helpful comment

bluebird was getting loaded by other third party libraries, so this didn't work for me, not sure why, maybe the other libs are overriding this config at some point.

require('bluebird').config({
  warnings: {
    wForgottenReturn: false
  }
});

However, this did

process.env.BLUEBIRD_W_FORGOTTEN_RETURN = 0;

source: http://bluebirdjs.com/docs/api/promise.config.html

All 9 comments

it says that all the options are false by default, but I get the warnings while not explicitly set {warnings: false}

Did you run them in Node.js with certain env variables set?

Did you run them in Node.js

In Node.js all is ok, it is only browser problem I guess

.done doesn't return a promise that can be chained so nobody can accidentally depend on the returned promise to be resolved.

.then returns a promise and you are even depending on the promise by using .done, that's why there is warning.

This

function withoutReturn() {
    Bluebird
        .resolve()
        .then(() => {
            Bluebird.delay(5000)
        })
        .done(function() {
            // Called immediately instead of 5 seconds later
        });
}

Is different from when the promise is returned:

function withReturn() {
    Bluebird
        .resolve()
        .then(() => {
            // Note the return
            return Bluebird.delay(5000)
        })
        .done(function() {
            // Called 5 seconds later
        });
}

The warning in the first case is because it is assumed the second case is what is wanted - that the callback is called 5 seconds later and not immediately.

Hi @petkaantonov, thanks for answer.

.done doesn't return a promise that can be chained so nobody can accidentally depend on the returned promise to be resolved.

function withoutReturnButDone() {
    Bluebird
        .resolve()
        .then(() => {
            Bluebird.resolve() // promise2
                  .done();
        }) // promise1
        .done();
}

But I do not want to promise1 to be dependend on promise2, and I guess that .done() says about it. I want to promise2 resolution to be async to resolution of promise1

Can we please have a way to silence these warnings? I am seeing them all over the place now, even in 3rd party libraries, because I've set bluebird as global.Promise.

@golyshevd https://github.com/petkaantonov/bluebird/blob/master/docs/docs/warning-explanations.md#warning-a-promise-was-created-in-a-handler-but-was-not-returned-from-it

If you know what you're doing and don't want to silence all warnings, you can create runaway promises without causing this warning by returning null

@Ginden Thanks, I didn't know that

If you know what you're doing and don't want to silence all warnings, you can create runaway promises without causing this warning by returning null

I did pick up on that, but that's hardly a solution either as it requires me to refactor a lot of my code, and doesn't solve the issue with warnings originating from 3rd party libraries.

In the end I found this to disable these pesky warnings:

Promise.config({
    // Enables all warnings except forgotten return statements.
    warnings: {
        wForgottenReturn: false
    }
});

bluebird was getting loaded by other third party libraries, so this didn't work for me, not sure why, maybe the other libs are overriding this config at some point.

require('bluebird').config({
  warnings: {
    wForgottenReturn: false
  }
});

However, this did

process.env.BLUEBIRD_W_FORGOTTEN_RETURN = 0;

source: http://bluebirdjs.com/docs/api/promise.config.html

Was this page helpful?
0 / 5 - 0 ratings