Search code examples
javascriptpromisebluebird

How do I promisify node-adodb?


I'm trying to promisify node-adodb using bluebirdjs.

I've tried this:

import Promise from 'bluebird'
import ADODB from 'node-adodb'

const db = ADODB.open(`...`)
const dbQuery = db.query(`...`)
const dbQueryOn = Promise.promisify(dbQuery.on, { context: dbQuery })

dbQueryOn('done').then(data => {
  console.log('data =', data)
}).catch(err => {
  console.log('err =', err)
})

The data is returned, but it comes via the .catch() not the .then() method.

How do I get node-adodb working with promises..?


Solution

  • I'm not familiar with node-adodb, but from its documentation it seems it uses a non-conventional way of returning errors and results (using event-like emitters).

    Bluebird's promisify requires the regular Node.js callback convention (first argument represents errors, second argument represents the "result" value), so you can't use it in this situation.

    But you can wrap it yourself:

    const db = ADODB.open(`...`);
    
    const runQuery = query => {
      return new Promise((resolve, reject) => {
        db.query(query)
          .on('done', resolve)
          .on('fail', reject);
      });
    }
    
    // Usage:
    runQuery(`...`).then(data => {
      console.log('data =', data)
    }).catch(err => {
      console.log('err =', err)
    })