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

Add promise support #44

Merged
merged 2 commits into from
Apr 25, 2016
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
11 changes: 8 additions & 3 deletions lib/remote-connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

var assert = require('assert');
var remoting = require('strong-remoting');
var utils = require('loopback-datasource-juggler/lib/utils');
var jutil = require('loopback-datasource-juggler/lib/jutil');
var RelationMixin = require('./relations');
var InclusionMixin = require('loopback-datasource-juggler/lib/include');
Expand Down Expand Up @@ -94,14 +95,18 @@ function createProxyMethod(Model, remotes, remoteMethod) {
var callback;
if (lastArgIsFunc) {
callback = args.pop();
} else {
callback = utils.createPromiseCallback();
}

if (remoteMethod.isStatic) {
return remotes.invoke(remoteMethod.stringName, args, callback);
remotes.invoke(remoteMethod.stringName, args, callback);
} else {
var ctorArgs = [this.id];
remotes.invoke(remoteMethod.stringName, ctorArgs, args, callback);
}

var ctorArgs = [this.id];
return remotes.invoke(remoteMethod.stringName, ctorArgs, args, callback);
return callback.promise;
}

scope[remoteMethod.name] = remoteMethodProxy;
Expand Down
9 changes: 9 additions & 0 deletions test/models.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ describe('Model tests', function() {
});
});

describe('Model methods', function() {
it('should support promises', function(done) {
assert(User.create() instanceof Promise);
assert(User.find() instanceof Promise);
assert(User.findById(99) instanceof Promise);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I think this may not work with loopback@3 where don't use the global Promise implementation. A better check would be something like this:

var retval = User.findById();
assert(retval && typeof retval.then === 'function');

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback, @bajtos.

done();
});
});

describe('Model.create([data], [callback])', function() {
it('should create an instance and save to the attached data source',
function(done) {
Expand Down