Esa*_*ija 23
2.0分支文档包含更好的宣传指南https://github.com/petkaantonov/bluebird/blob/master/API.md#promisification
它实际上有mongodb示例,它更简单:
var Promise = require("bluebird");
var MongoDB = require("mongodb");
Promise.promisifyAll(MongoDB);
Run Code Online (Sandbox Code Playgroud)
Dmi*_*sky 18
使用时Promise.promisifyAll(),如果必须实例化目标对象,则有助于识别目标原型.对于MongoDB JS驱动程序,标准模式是:
Db使用MongoClient静态方法或Db构造函数获取对象Db#collection()来获取一个Collection对象.因此,借用/sf/answers/1521341251/,您可以:
var Promise = require('bluebird');
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
var Collection = mongodb.Collection;
Promise.promisifyAll(Collection.prototype);
Promise.promisifyAll(MongoClient);
Run Code Online (Sandbox Code Playgroud)
现在你可以:
var client = MongoClient.connectAsync('mongodb://localhost:27017/test')
.then(function(db) {
return db.collection("myCollection").findOneAsync({ id: 'someId' })
})
.then(function(item) {
// Use `item`
})
.catch(function(err) {
// An error occurred
});
Run Code Online (Sandbox Code Playgroud)
这会让你走得很远,除了它还有助于确保Cursor返回的对象Collection#find()也是有效的.在MongoDB JS驱动程序中,返回的游标Collection#find()不是从原型构建的.因此,您可以包装方法并每次都传播游标.如果您不使用游标,或者不想产生开销,则不需要这样做.这是一种方法:
Collection.prototype._find = Collection.prototype.find;
Collection.prototype.find = function() {
var cursor = this._find.apply(this, arguments);
cursor.toArrayAsync = Promise.promisify(cursor.toArray, cursor);
cursor.countAsync = Promise.promisify(cursor.count, cursor);
return cursor;
}
Run Code Online (Sandbox Code Playgroud)
All*_*ood 10
我知道这已经多次回答了,但我想补充一点关于这个主题的更多信息.根据Bluebird自己的文档,您应该使用'using'来清理连接并防止内存泄漏. 蓝鸟资源管理
我整个地方都看到了如何正确地做到这一点,信息很少,所以我想我会分享经过多次试验和错误后发现的东西.我在下面使用的数据(餐馆)来自MongoDB样本数据.你可以在这里得到它:MongoDB导入数据
// Using dotenv for environment / connection information
require('dotenv').load();
var Promise = require('bluebird'),
mongodb = Promise.promisifyAll(require('mongodb'))
using = Promise.using;
function getConnectionAsync(){
// process.env.MongoDbUrl stored in my .env file using the require above
return mongodb.MongoClient.connectAsync(process.env.MongoDbUrl)
// .disposer is what handles cleaning up the connection
.disposer(function(connection){
connection.close();
});
}
// The two methods below retrieve the same data and output the same data
// but the difference is the first one does as much as it can asynchronously
// while the 2nd one uses the blocking versions of each
// NOTE: using limitAsync seems to go away to never-never land and never come back!
// Everything is done asynchronously here with promises
using(
getConnectionAsync(),
function(connection) {
// Because we used promisifyAll(), most (if not all) of the
// methods in what was promisified now have an Async sibling
// collection : collectionAsync
// find : findAsync
// etc.
return connection.collectionAsync('restaurants')
.then(function(collection){
return collection.findAsync()
})
.then(function(data){
return data.limit(10).toArrayAsync();
});
}
// Before this ".then" is called, the using statement will now call the
// .dispose() that was set up in the getConnectionAsync method
).then(
function(data){
console.log("end data", data);
}
);
// Here, only the connection is asynchronous - the rest are blocking processes
using(
getConnectionAsync(),
function(connection) {
// Here because I'm not using any of the Async functions, these should
// all be blocking requests unlike the promisified versions above
return connection.collection('restaurants').find().limit(10).toArray();
}
).then(
function(data){
console.log("end data", data);
}
);
Run Code Online (Sandbox Code Playgroud)
我希望这可以帮助其他想要通过蓝鸟书做事的人.
版本1.4.9 mongodb现在应该很容易实现:
Promise.promisifyAll(mongo.Cursor.prototype);
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅https://github.com/mongodb/node-mongodb-native/pull/1201.
| 归档时间: |
|
| 查看次数: |
17393 次 |
| 最近记录: |