Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bluebird — a promise was created in a handler but was not returned from it

First of all, I know that I have to return promises to avoid this warning. I've also tried returning null as suggested here in the docs. Consider this piece of code, I'm using it in Mongoose's pre-save hook, but I've experienced this warning in other places:

var Story = mongoose.model('Story', StorySchema);

StorySchema.pre('save', function(next) {
    var story = this;

    // Fetch all stories before save to automatically assign
    // some variable, avoiding conflict with other stories
    return Story.find().then(function(stories) {

        // Some code, then set story.somevar value
        story.somevar = somevar;
        return null;
    }).then(next).catch(next); // <-- this line throws warning
});

I've also tried (initially) this way:

        story.somevar = somevar;
        return next(); // <-- this line throws warning
    }).catch(next);

But it doesn't work either. Oh, and I have to mention, that I use Bluebird:

var Promise = require('bluebird'),
    mongoose = require('mongoose');

mongoose.Promise = Promise;

Not a duplicate of A promise was created in a handler but was not returned from it, the guy forgot to return a promise.

like image 614
Anton Egorov Avatar asked Dec 01 '25 09:12

Anton Egorov


1 Answers

The problem is pretty much using a next callback at all, which calls functions that create promises without returning them. Ideally the hooks just needed to return promises instead of taking callbacks.

You should be able to prevent the warning by using

.then(function(result) {
    next(null, result);
    return null;
}, function(error) {
    next(error);
    return null;
});
like image 129
Bergi Avatar answered Dec 03 '25 23:12

Bergi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!