Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow promises to resolve with a single arg #180

Merged
merged 1 commit into from
Feb 13, 2015
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
1 change: 1 addition & 0 deletions lib/shared-method.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
42 changes: 42 additions & 0 deletions test/shared-method.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down