Skip to content Skip to sidebar Skip to footer

How To Use Node Sqlite3 With Q (promise)

I'm trying to use the promise with sqlite3. Here is a part of my source code: this.deleteTag = function(tag, project){ var db = this.db; if (project){ return q.nfca

Solution 1:

I think db.run is not function but method. from Q doc:

If you are working with methods, instead of simple functions, you can easily run in to the usual problems where passing a method to another function—like Q.nfcall—"un-binds" the method from its owner. To avoid this, you can either use Function.prototype.bind or some nice shortcut methods we provide:

return Q.ninvoke(redisClient, "get", "user:1:id");
return Q.npost(redisClient, "get", ["user:1:id"]);

But I always use Q.denodeify or Q.nbind. it is cleaner.

You can also create reusable wrappers with Q.denodeify or Q.nbind:

var readFile = Q.denodeify(FS.readFile);
return readFile("foo.txt", "utf-8");

var redisClientGet = Q.nbind(redisClient.get, redisClient);
return redisClientGet("user:1:id");

Solution 2:

You could bind the object that you created when you promisify the function.

Example

const { promisify } = require('util');
const db = new lib_sqlite3.Database(_dirname + '/your-db-path');

const runAsync = promisify(db.run.bind(db));


Post a Comment for "How To Use Node Sqlite3 With Q (promise)"