What is the correct way of using Bluebird for Mongoose promises?
NickName:Özgür Uysal Ask DateTime:2016-11-11T23:50:56

What is the correct way of using Bluebird for Mongoose promises?

I've been reading documentaion and articles and everyone seems to describe a different way about using Mongoose and Bluebird together. Even the official Mongoose documentation says something and Bluebird documentaion says another thing.

According to Mongoose:

mongoose.Promise = require('bluebird');

According to Bluebird:

var Promise = require("bluebird");
Promise.promisifyAll(require("mongoose"));

So to my understanding, if you pick the Mongoose way a sample query would be like:

User.findById('someId')
    .then(function(){
        // do stuff
    })
    .catch(function(err){
        // handle error
    })

But also in Mongoose docs it says that:

Mongoose queries are not promises. However, they do have a .then() function for yield and async/await. If you need a fully-fledged promise, use the .exec() function.

So in this case, is .then above a promise or not?

If you go with Bluebird way:

User.findById('someId')
    .execAsync()
    .then(function(){
        // do stuff
    })
    .catch(function(err){
        // handle error
    })

Or maybe even skip execAsync() and start with findByIdAsync instead.

Really confused with different documentaion. I'd appreciate if someone can shed some light into this.

Copyright Notice:Content Author:「Özgür Uysal」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/40551550/what-is-the-correct-way-of-using-bluebird-for-mongoose-promises

Answers
robertklep 2016-11-26T20:23:10

Choose the Mongoose way:\n\nmongoose.Promise = require('bluebird');\n\n\nThat's because Mongoose already supports promises (besides also accepting callbacks); the above code just replaces Mongoose's own promise library (mpromise) by Bluebird (which is probably faster, better tested, and has more utility functions available).\n\nBluebird's promisify*() is meant to allow code that doesn't already use promises (purely callback-based functions) to return promises.",


stasovlas 2016-11-22T19:14:58

From Bluebird doc \n\nPromise.promisifyAll(\n Object target,\n [Object {\n suffix: String=\"Async\",\n multiArgs: boolean=false,\n filter: boolean function(String name, function func, Object target, boolean passesDefaultFilter),\n promisifier: function(function originalFunction, function defaultPromisifier)\n } options] ) -> Object\n\n\nas you can see, by default, promisifyAll add suffix 'Asyns', so if you are using this way of promisification:\n\nvar Promise = require(\"bluebird\");\nPromise.promisifyAll(require(\"mongoose\"));\n\n\nthe async version of User.findById will be User.findByIdAsync\n\nwhat about mongoose.Promise\n\nthen you use promise library like \n\nmongoose.Promise = require('bluebird');\n\n\nbuilt-in promise mechanism replaced by library: query.exec().constructor replaced by require('bluebird') so instead .exec() for return promise, you can use bluebird probabilities directly for mongoose queries like \n\nUser.findOne({}).then(function(user){\n // ..\n}) \n",


More about “What is the correct way of using Bluebird for Mongoose promises?” related questions

What is the correct way of using Bluebird for Mongoose promises?

I've been reading documentaion and articles and everyone seems to describe a different way about using Mongoose and Bluebird together. Even the official Mongoose documentation says something and Bl...

Show Detail

How to do a findOneAndUpdate with bluebird promises (mongoose)

I cannot find any examples on how to resolve promises with bluebird when using mongoose's findOneAndUpdate. var Promise = require("bluebird"); var mongoose = Promise.promisifyAll(require('mongoose...

Show Detail

Parallel mongoose queries with promises/bluebird?

I have a nodejs/mongodb project using mongoose, and also bluebird for promises. Here is my problem : I have a function that has to execute sometimes 1 query, sometimes 2 queries in parallel, and t...

Show Detail

Mongoose promises and bluebird

Will node ever have a builtin implementation of Promise or should we adopt one of the major libraries? such as Q/Blueblird? I have a code which uses async.waterfall and I want to move it to use pro...

Show Detail

Bluebird promises returning too early in express/mongoose

I'm new to promises and wondering why the following is not working. I'm assuming because I need to set up my custom methods a specific way to work with Promises. I'm using bluebird as my Promise l...

Show Detail

Mongoose giving mpromise deprecation warning despite bluebird?

I am constantly getting the following error while compiling my Node-Mongoose (NextJS) app: DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in yo...

Show Detail

Is it necessary to put async in bluebird promises

I am trying to add bluebird promises in the project. I am using NodeJS, Express, Mongodb. This is my sample model file const mongoose = require('mongoose') // Blue Bird mongoose.Promise =

Show Detail

Mongoose: ".find(...).exec(...).then(...).catch(...).finally is not a function" using bluebird?

I am currently trying to use Promises with Mongoose. I've read that as of 4.1, mPromise had been added as well as the the ability to plug external promises libraries such as bluebird or q. I have...

Show Detail

JavaScript Promises mongoose and bluebird missing catch and fail

I've started using promises, I use Node.js Mango (with mongoose) and bluebird.. The issue I'm having is for some reason when I chain the mongoose call with functions returning promises (I'm assumin...

Show Detail

Mongoose Promise with bluebird and typescript

I am developing an application with nodejs/typescript and a mongodb database. To query the database, I am using mongoose. I've just read an article from the mongoose documentation on how to plug i...

Show Detail