Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions packages/common/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,13 +687,18 @@ util.getUserAgentFromPackageJson = getUserAgentFromPackageJson;
* Wraps a callback style function to conditionally return a promise.
*
* @param {function} originalMethod - The method to promisify.
* @param {object=} options - Promise options.
* @param {boolean} options.singular - Resolve the promise with single arg

This comment was marked as spam.

This comment was marked as spam.

* instead of an array.
* @return {function} wrapped
*/
function promisify(originalMethod) {
function promisify(originalMethod, options) {
if (originalMethod.promisified_) {
return originalMethod;
}

options = options || {};

var slice = Array.prototype.slice;

var wrapper = function() {
Expand Down Expand Up @@ -723,7 +728,11 @@ function promisify(originalMethod) {
return reject(err);
}

resolve(callbackArgs);
if (options.singular && callbackArgs.length === 1) {

This comment was marked as spam.

This comment was marked as spam.

resolve(callbackArgs[0]);
} else {
resolve(callbackArgs);
}
});

originalMethod.apply(context, args);
Expand Down Expand Up @@ -758,7 +767,7 @@ function promisifyAll(Class, options) {
var originalMethod = Class.prototype[methodName];

if (!originalMethod.promisified_) {
Class.prototype[methodName] = util.promisify(originalMethod);
Class.prototype[methodName] = util.promisify(originalMethod, options);
}
});
}
Expand Down
47 changes: 47 additions & 0 deletions packages/common/test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,25 @@ describe('common/util', function() {
assert(FakeClass2.prototype.method.promisified_);
});

it('should pass the options object to promisify', function(done) {
var promisify = util.promisify;
var fakeOptions = {
a: 'a'
};

util.promisify = function(method, options) {
assert.strictEqual(method, FakeClass2.prototype.method);
assert.strictEqual(options, fakeOptions);
util.promisify = promisify;
done();
};

function FakeClass2() {}
FakeClass2.prototype.method = function() {};

util.promisifyAll(FakeClass2, fakeOptions);
});

it('should not re-promisify methods', function() {
var method = FakeClass.prototype.methodName;

Expand Down Expand Up @@ -1643,5 +1662,33 @@ describe('common/util', function() {

assert(promise instanceof FakePromise);
});

it('should resolve singular arguments', function() {
var fakeArg = 'hi';

func = util.promisify(function(callback) {
callback.apply(this, [null, fakeArg]);
}, {
singular: true
});

return func().then(function(arg) {
assert.strictEqual(arg, fakeArg);
});
});

it('should ignore singular when multiple args are present', function() {
var fakeArgs = ['a', 'b'];

func = util.promisify(function(callback) {
callback.apply(this, [null].concat(fakeArgs));
}, {
singular: true
});

return func().then(function(args) {
assert.deepEqual(args, fakeArgs);
});
});
});
});