Skip to content

promisify() behavior with multiple parameters can break compatibility #307

@julien-f

Description

@julien-f

Current behavior of promisify is to resolve to an array when there is more than one result to the callback.

Unfortunately, if a function is changed to return two or more results instead of one, it breaks the compatibility of the promisified version.

var assert = require('assert');
var promisify = require('bluebird').promisify;

// Original function returning one result.
function version1(cb) {
  process.nextTick(function () {
    cb(null, 'foo');
  });
}

// Updated function returning two results.
function version2(cb) {
  process.nextTick(function () {
    cb(null, 'foo', 'bar');
  });
}

// The compatibility is maintained when using callbacks.
function validateWithCallback(fn) {
  fn(function (err, foo) {
    assert.equal(foo, 'foo');
  });
}
validateWithCallback(version1);
validateWithCallback(version2);

// The compatibility is broken when using the promisified version.
function validateWithPromise(fn) {
  promisify(fn)().then(function (foo) {
    assert.equal(foo, 'foo');
  });
}
validateWithPromise(version1);
validateWithPromise(version2);

I think the correct approach would be to have an option to force to either return only the first result or to always return an array.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions