将 Bluebird 用于 Mongoose promises 的正确方法是什么?
What is the correct way of using Bluebird for Mongoose promises?
我一直在阅读文档和文章,似乎每个人都描述了一起使用 Mongoose 和 Bluebird 的不同方式。甚至官方 Mongoose 文档 says something and Bluebird documentaion says another thing.
根据猫鼬的说法:
mongoose.Promise = require('bluebird');
根据蓝鸟的说法:
var Promise = require("bluebird");
Promise.promisifyAll(require("mongoose"));
据我了解,如果您选择 Mongoose 方式,示例查询将如下所示:
User.findById('someId')
.then(function(){
// do stuff
})
.catch(function(err){
// handle error
})
但在 Mongoose 文档中它也说:
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.
所以在这种情况下,.then
是否高于承诺?
如果你选择蓝鸟方式:
User.findById('someId')
.execAsync()
.then(function(){
// do stuff
})
.catch(function(err){
// handle error
})
或者甚至可以跳过 execAsync()
而从 findByIdAsync
开始。
真的对不同的文档感到困惑。如果有人可以阐明这一点,我将不胜感激。
来自蓝鸟文档
Promise.promisifyAll(
Object target,
[Object {
suffix: String="Async",
multiArgs: boolean=false,
filter: boolean function(String name, function func, Object target, boolean passesDefaultFilter),
promisifier: function(function originalFunction, function defaultPromisifier)
} options] ) -> Object
如你所见,promisifyAll默认添加后缀'Asyns',所以如果你使用这种promisification方式:
var Promise = require("bluebird");
Promise.promisifyAll(require("mongoose"));
User.findById
的异步版本将是 User.findByIdAsync
那mongoose.Promise
呢
然后你使用像
这样的 promise 库
mongoose.Promise = require('bluebird');
内置承诺机制被库取代:query.exec().constructor
被require('bluebird')
取代所以.exec()
代替return承诺,你可以直接使用bluebird概率进行猫鼬查询喜欢
User.findOne({}).then(function(user){
// ..
})
选择 Mongoose 方式:
mongoose.Promise = require('bluebird');
那是因为 Mongoose 已经 支持承诺(除了还接受回调);上面的代码只是用 Bluebird 替换了 Mongoose 自己的 promise 库 (mpromise
)(这可能更快,经过更好的测试,并且有更多可用的实用函数)。
Bluebird 的 promisify*()
旨在允许尚未 的代码 使用承诺(纯粹基于回调的函数)到 return 承诺。
对于那些使用 TypeScript 的人来说,正确的方法是:
(<any>mongoose).Promise = YOUR_PROMISE;
来自文档:
Typescript does not allow assigning properties of imported modules. To avoid compile errors use one of the options below in your code:
(<any>mongoose).Promise = YOUR_PROMISE;
require('mongoose').Promise = YOUR_PROMISE;
import mongoose = require('mongoose'); mongoose.Promise = YOUR_PROMISE;
我一直在阅读文档和文章,似乎每个人都描述了一起使用 Mongoose 和 Bluebird 的不同方式。甚至官方 Mongoose 文档 says something and Bluebird documentaion says another thing.
根据猫鼬的说法:
mongoose.Promise = require('bluebird');
根据蓝鸟的说法:
var Promise = require("bluebird");
Promise.promisifyAll(require("mongoose"));
据我了解,如果您选择 Mongoose 方式,示例查询将如下所示:
User.findById('someId')
.then(function(){
// do stuff
})
.catch(function(err){
// handle error
})
但在 Mongoose 文档中它也说:
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.
所以在这种情况下,.then
是否高于承诺?
如果你选择蓝鸟方式:
User.findById('someId')
.execAsync()
.then(function(){
// do stuff
})
.catch(function(err){
// handle error
})
或者甚至可以跳过 execAsync()
而从 findByIdAsync
开始。
真的对不同的文档感到困惑。如果有人可以阐明这一点,我将不胜感激。
来自蓝鸟文档
Promise.promisifyAll(
Object target,
[Object {
suffix: String="Async",
multiArgs: boolean=false,
filter: boolean function(String name, function func, Object target, boolean passesDefaultFilter),
promisifier: function(function originalFunction, function defaultPromisifier)
} options] ) -> Object
如你所见,promisifyAll默认添加后缀'Asyns',所以如果你使用这种promisification方式:
var Promise = require("bluebird");
Promise.promisifyAll(require("mongoose"));
User.findById
的异步版本将是 User.findByIdAsync
那mongoose.Promise
呢然后你使用像
这样的 promise 库mongoose.Promise = require('bluebird');
内置承诺机制被库取代:query.exec().constructor
被require('bluebird')
取代所以.exec()
代替return承诺,你可以直接使用bluebird概率进行猫鼬查询喜欢
User.findOne({}).then(function(user){
// ..
})
选择 Mongoose 方式:
mongoose.Promise = require('bluebird');
那是因为 Mongoose 已经 支持承诺(除了还接受回调);上面的代码只是用 Bluebird 替换了 Mongoose 自己的 promise 库 (mpromise
)(这可能更快,经过更好的测试,并且有更多可用的实用函数)。
Bluebird 的 promisify*()
旨在允许尚未 的代码 使用承诺(纯粹基于回调的函数)到 return 承诺。
对于那些使用 TypeScript 的人来说,正确的方法是:
(<any>mongoose).Promise = YOUR_PROMISE;
来自文档:
Typescript does not allow assigning properties of imported modules. To avoid compile errors use one of the options below in your code:
(<any>mongoose).Promise = YOUR_PROMISE;
require('mongoose').Promise = YOUR_PROMISE;
import mongoose = require('mongoose'); mongoose.Promise = YOUR_PROMISE;