gpt4 book ai didi

javascript - 有没有正确的方法来 promisify arangojs?

转载 作者:行者123 更新时间:2023-12-03 10:22:38 25 4
gpt4 key购买 nike

我想在我的项目中使用 arangojs 3.4.2。从 3.0 开始,驱动程序不再使用任何 promise 。在尝试了几个库来 promise 驱动程序后,我没有成功(bluebird,promisify-node...):每次驱动程序返回一个新实例时,该实例都不会被 promise ,并且我必须再次 promise 新实例才能与 promise 一起使用:

var Promise=require('bluebird');
var arango=require('arangojs');
db=Promise.promisifyAll(new arango("http://localhost:8529"));
/*db is promisified properly*/
testdb=db.databaseAsync('test').then(function(testInstance){
/*
the testInstance returned by the driver is not promisified
to use it with promises i've to promisify again
*/
})

有办法实现吗?

最佳答案

这是正确的。

如果你想promisify驱动程序中所有对象的所有方法,你需要直接promisify原型(prototype)的方法:

var Database = require('arangojs/lib/Database');
Promise.promisifyAll(Database.prototype);

var db = new Database('http://localhost:8529');
db.databasesAsync().then(function (databases) {
databases.forEach(function (database) {
assertTrue(typeof database.databaseAsync === 'function');
});
});

关于javascript - 有没有正确的方法来 promisify arangojs?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29557522/

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