From ebd8660e5239dbeaa5177bd3ee51f3d4bdc6ccf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Fri, 13 Feb 2015 10:16:16 -0800 Subject: [PATCH] Allow promises to resolve with a single arg --- lib/shared-method.js | 1 + test/shared-method.test.js | 42 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/lib/shared-method.js b/lib/shared-method.js index 67e189f..11d2ec8 100644 --- a/lib/shared-method.js +++ b/lib/shared-method.js @@ -246,6 +246,7 @@ SharedMethod.prototype.invoke = function(scope, args, remotingOptions, cb) { if (retval && typeof retval.then === 'function') { return retval.then( function(args) { + if (returns.length === 1) args = [args]; var result = SharedMethod.toResult(returns, args); debug('- %s - promise result %j', sharedMethod.name, result); cb(null, result); diff --git a/test/shared-method.test.js b/test/shared-method.test.js index 5335602..5e95726 100644 --- a/test/shared-method.test.js +++ b/test/shared-method.test.js @@ -106,6 +106,48 @@ describe('SharedMethod', function() { }); }); + it('handles promise resolved with a single arg', function(done) { + var method = givenSharedMethod( + function() { + return new Promise(function(resolve, reject) { + resolve('data'); + }); + }, + { + returns: [ + { arg: 'value', type: 'string' }, + ] + }); + + method.invoke('ctx', {}, function(err, result) { + setImmediate(function() { + expect(result).to.eql({ value: 'data' }); + done(); + }); + }); + }); + + it('handles promise resolved with a single array arg', function(done) { + var method = givenSharedMethod( + function() { + return new Promise(function(resolve, reject) { + resolve(['a', 'b']); + }); + }, + { + returns: [ + { arg: 'value', type: ['string']}, + ] + }); + + method.invoke('ctx', {}, function(err, result) { + setImmediate(function() { + expect(result).to.eql({ value: ['a', 'b'] }); + done(); + }); + }); + }); + it('handles rejected promise returned from the method', function(done) { var testError = new Error('expected test error'); var method = givenSharedMethod(function() {