From b8b92fbeda9770a244300b9e35510119181d18da Mon Sep 17 00:00:00 2001 From: Tim van der Staaij Date: Thu, 15 Sep 2016 23:33:56 +0200 Subject: [PATCH] Fix support for remote hooks returning a Promise Fix beforeRemote/afterRemote to correctly return promises returned by the user-provided hook callback. --- lib/model.js | 4 ++-- test/model.test.js | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/model.js b/lib/model.js index f3fd0bb7..a85f4e2f 100644 --- a/lib/model.js +++ b/lib/model.js @@ -202,7 +202,7 @@ module.exports = function(registry) { this._runWhenAttachedToApp(function(app) { var remotes = app.remotes(); remotes.before(className + '.' + name, function(ctx, next) { - fn(ctx, ctx.result, next); + return fn(ctx, ctx.result, next); }); }); }; @@ -213,7 +213,7 @@ module.exports = function(registry) { this._runWhenAttachedToApp(function(app) { var remotes = app.remotes(); remotes.after(className + '.' + name, function(ctx, next) { - fn(ctx, ctx.result, next); + return fn(ctx, ctx.result, next); }); }); }; diff --git a/test/model.test.js b/test/model.test.js index f7c8d079..87d1c5fb 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -10,6 +10,7 @@ var loopback = require('../'); var ACL = loopback.ACL; var defineModelTestsWithDataSource = require('./util/model-tests'); var PersistedModel = loopback.PersistedModel; +var Promise = require('bluebird'); var sinonChai = require('sinon-chai'); chai.use(sinonChai); @@ -308,6 +309,32 @@ describe.onServer('Remote Methods', function() { done(); }); }); + + it('Does not stop the hook chain after returning a promise', function(done) { + var hooksCalled = []; + + User.beforeRemote('create', function() { + hooksCalled.push('first'); + return Promise.resolve(); + }); + + User.beforeRemote('create', function(ctx, user, next) { + hooksCalled.push('second'); + next(); + }); + + // invoke save + request(app) + .post('/users') + .send({ data: { first: 'foo', last: 'bar' }}) + .expect('Content-Type', /json/) + .expect(200) + .end(function(err, res) { + if (err) return done(err); + expect(hooksCalled).to.eql(['first', 'second']); + done(); + }); + }); }); describe('Model.afterRemote(name, fn)', function() {