From a4e825191f05a2bc5e14dbcd1438ec60bee1c7d2 Mon Sep 17 00:00:00 2001 From: Heath Morrison Date: Mon, 25 Apr 2016 01:07:15 +0300 Subject: [PATCH 1/2] Added promise support --- lib/remote-connector.js | 9 +++++++-- test/models.test.js | 9 +++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/remote-connector.js b/lib/remote-connector.js index 848c9d5..39f8514 100644 --- a/lib/remote-connector.js +++ b/lib/remote-connector.js @@ -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'); @@ -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); + return callback.promise; } var ctorArgs = [this.id]; - return remotes.invoke(remoteMethod.stringName, ctorArgs, args, callback); + remotes.invoke(remoteMethod.stringName, ctorArgs, args, callback); + return callback.promise; } scope[remoteMethod.name] = remoteMethodProxy; diff --git a/test/models.test.js b/test/models.test.js index 879b516..9d6b659 100644 --- a/test/models.test.js +++ b/test/models.test.js @@ -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); + done(); + }); + }); + describe('Model.create([data], [callback])', function() { it('should create an instance and save to the attached data source', function(done) { From 75fd2f42f30da9ed096066ed9a33b0242e4400d8 Mon Sep 17 00:00:00 2001 From: Heath Morrison Date: Mon, 25 Apr 2016 01:11:05 +0300 Subject: [PATCH 2/2] Small logic cleanup --- lib/remote-connector.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/remote-connector.js b/lib/remote-connector.js index 39f8514..b6024bd 100644 --- a/lib/remote-connector.js +++ b/lib/remote-connector.js @@ -101,11 +101,11 @@ function createProxyMethod(Model, remotes, remoteMethod) { if (remoteMethod.isStatic) { remotes.invoke(remoteMethod.stringName, args, callback); - return callback.promise; + } else { + var ctorArgs = [this.id]; + remotes.invoke(remoteMethod.stringName, ctorArgs, args, callback); } - var ctorArgs = [this.id]; - remotes.invoke(remoteMethod.stringName, ctorArgs, args, callback); return callback.promise; }