简体   繁体   中英

How to promisify your own javascript api which uses another promisified api with Bluebird?

I'm struggling with this a bit. I want to build an abstraction layer over a database driver module in node. I've already promisified the driver module by doing the following:

var sql = Promise.promisifyAll(require('sql-driver'));

Then within my own js file I want to wrap queries like so:

function query(queryString, transaction) {
   sql.connectAsync(config).then(function() {

      var req = new sql.Request(transaction);
      req.queryAsync(queryString).then(function(resultSet)) {
          console.log(resultSet);
      });
   });
}

How can I promisify (using bluebird) so that my query function is also promisified and returns a promise or thenable?

Update:

I want my wrapper method to also be promisified so that the user can do something like this:

var myDbWrapper = require('my-db-wapper');
function getData() {
    myDbWrapper.startTransaction()
    .then(function(transaction) {
        return myDbWrapper.query('select 1 as number', transaction);
     })
    .then(function(resultSet) {
        console.log(resultSet);
     }).
    .catch(function(e) {
        console.error(e);
    });
}

I'm sure this isn't working code, just trying to convey the idea. Also, this example only shows one query and doesn't show the commit method, which is also part of the wrapper. Can someone show me an example use promises to do a whole transaction with more than one query and then a 'commit' if no errors?

Update 2:

idbehold's example below won't allow me to do this type of thing assuming that we don't care about a transaction at this time, just a simple query:

myDbWrapper.query(queryString, undefined)
.then(function(recordSet) {
   console.log(recordSet);
})
.catch(function(err) {
   console.error(err);
});

How can I enable my query method to work that way?

Update 3:

I've found my answer. See below.

Just return that promise:

function query(queryString, transaction) {
   return sql.connectAsync(config).then(function() {
      var req = new sql.Request(transaction);
      return req.queryAsync(queryString);
   }).then(function(resultSet)) {        // You don't actually
      console.log(resultSet);            // need this this part
      return resultSet;                  // unless you need to log
   });
}

Ok, I figured it out and it was right in front of my face. It was my lack of understanding about Promises. Most responses were correct about returning a promise, but I shouldn't return the promise I'm using, I need to create a new promise like so:

function query (queryString, transaction) {
   return new Promise(function(resolve, reject) {
        var req = new sql.Request(transaction);
        req.queryAsync(queryString).then(function(resultSet) {
            resolve(resultSet);
        })
        .catch(function(err) {
            reject(err);
        });
    });
}

Edit, without creating a new promise:

function query (queryString, transaction) {
  var req = new sql.Request(transaction);
  return req.queryAsync(queryString);
}

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