简体   繁体   中英

Is there a proper way to promisify arangojs?

I'd like to use arangojs 3.4.2 in my project. Since 3.0 there are no promises used by the driver. After trying several libs to promisify the driver i have no success (bluebird,promisify-node...): each time the driver returns a new instance that instance is not promisified, and i have to promisify the new instance again to use with promises:

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
    */
})

Is there a way to achieve this?

That is correct.

If you want to promisify all the methods of all the objects in the driver, you need to promisify the prototypes' methods directly:

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');
  });
});

As of version 3.5, if the global Promise constructor is defined when an asynchronous function is called, the function will also return a promise.

https://github.com/arangodb/arangojs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM