gpt4 book ai didi

node.js - 是否仍然需要 promise MongoDB 驱动程序?

转载 作者:IT老高 更新时间:2023-10-28 13:30:04 26 4
gpt4 key购买 nike

这个问题的答案是否仍然相关:How can I promisify the MongoDB native Javascript driver using bluebird?

我不知道什么时候更新的,但是 MongoDB 的 2.0 JS 驱动程序在选项对象 promiseLibrary 中有一个属性:http://mongodb.github.io/node-mongodb-native/2.0/api/MongoClient.html

而且大多数方法/函数都会返回一个 promise ,例如 Cursor.toArray() .

但是,我找不到使用这个新选项的示例,但是使用它会不会更简单:

MongoClient.connect('mongodb://URL', { promiseLibrary: require('bluebird') });

或者这个定义是错误的? - 在这种情况下,应该如何正确定义?

更新:

代码在 io.js 上运行,所以我什至不需要指定 promiseLibrary,因为驱动程序会使用 ES6 promise ? - 然而,据说 bluebird 的 promises 更慢:

Why are native ES6 promises slower and more memory-intensive than bluebird?

更新 2:我已经添加了 bluebird 标签——如果 promisifying 真的比使用 MongoDB 自己的实现更好的话,也许 bluebird 的开发人员可以提供更多细节?

最佳答案

Is it still necessary to promisify the MongoDB driver?

不,他们现在已经内置了对 bluebird 等 promise 库的支持。我认为我们应该以不同的方式提出这个问题:

Is it a good idea to promisify the MongoDB driver?

也许,bluebird 在将回调 API 转换为 Promise 方面比 Mongo 驱动程序在内部通过更轻松地包装做得更好。仍然 promise “在以前的日子”可能会更快。

关于node.js - 是否仍然需要 promise MongoDB 驱动程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31916441/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com