Add custom then()-method to every function generated by promisifyAll

I've recently started playing around with Aerospike and their Node driver. I think the database itself is phenomenal, but the client library has a minor hiccup:

// Read the same record from database 
client.get(key, function(err, rec, meta) {
  // Check for errors 
  if ( err.code == status.AEROSPIKE_OK ) {
    // The record was successfully read. 
    console.log(rec, meta);
  } else {
    // An error occurred 
    console.error('error:', err);

Instead of returning null or undefined as the error when all went well, an object is always sent back. If err.code === 0 all went well. This means that the library can not be wrapped with bluebird's promisifyAll.

I tried to fix this by adding yet another then-method after each function generated by promisifyAll.

getPromise().then((err, rec, meta) => {
  if(err.code === 0) {
    return {rec: rec, meta: meta};
  } else {
    return Promise.reject();

I could not get it to work without explicitly doing it to every function manually. Is there any other way to achieve this?


Sounds like this is a case for Bluebird's custom promisifier option:

var Promise = require('bluebird');
var aerospike = Promise.promisifyAll(require('aerospike'), {
    promisifier: function(originalFunction, defaultPromisifer) {
        return defaultPromisifer(function() {
            var last = arguments.length-1;
            var callback = arguments[last];
            arguments[last] = function weirdAerospikeCallback(err, rec, meta) {
                if (!err || err.code == status.AEROSPIKE_OK)
                    callback(null, {rec: rec, meta: meta});
            originalFunction.apply(this, arguments);

Need Your Help

Playframework 2.2 with scala and sub-projects

scala playframework subproject

I have been trying to build up a project with some subprojects but I cannot get it to work as I want..

Get namespace in a static function

.net namespaces static-methods

In an instance method, I can easily find the executing namespace: