Creating a database as a callback?

15 views
Skip to first unread message

Kevin Burton

unread,
Dec 28, 2015, 8:13:00 AM12/28/15
to Node-Neo4j
I notice in the examples that I have seen the database handle is always created as something like:

var db = new neo4j.GraphDatabase();


This seems to be "non-node" like. Is there a form of this where the database and an error object is passed to a callback. Something like:

neo4j.CreateGraphDatabase(function(err, db) {


. . . . .


});

Thank you. 


Aseem Kishore

unread,
Dec 28, 2015, 12:34:14 PM12/28/15
to Kevin Burton, Node-Neo4j
Hi Kevin,

Creating the database handle doesn't require any network call or anything like that, which is why it returns immediately rather than via a callback.

This is similar to many other libraries for things beyond Neo4j. E.g. creating a handle to an S3 bucket with the AWS SDK, or an S3 client with Knox:


Any particular reason you need it to be returned via a callback?

Aseem


Kevin Burton

unread,
Dec 28, 2015, 1:39:55 PM12/28/15
to Node-Neo4j, ronald.ke...@gmail.com
No particular reason. It just seemed more consistent to use a callback. Also one less variable in global space.

Thank you.

Kevin

Aseem Kishore

unread,
Dec 28, 2015, 1:43:13 PM12/28/15
to Kevin Burton, Node-Neo4j
If you'd like, you can easily wrap it to use a callback:

function createGraphDatabase(opts, callback) {
    setImmediate(function () {
        try {
            var db = new neo4j.GraphDatabase(opts);
            callback(null, db);
        } catch (err) {
            callback(err, null);
        }
    });
}

Cheers,
Aseem

Reply all
Reply to author
Forward
0 new messages