Skip to content

Commit b8b92fb

Browse files
tvdstaaijbajtos
authored andcommitted
Fix support for remote hooks returning a Promise
Fix beforeRemote/afterRemote to correctly return promises returned by the user-provided hook callback.
1 parent 715bc1e commit b8b92fb

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

lib/model.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ module.exports = function(registry) {
202202
this._runWhenAttachedToApp(function(app) {
203203
var remotes = app.remotes();
204204
remotes.before(className + '.' + name, function(ctx, next) {
205-
fn(ctx, ctx.result, next);
205+
return fn(ctx, ctx.result, next);
206206
});
207207
});
208208
};
@@ -213,7 +213,7 @@ module.exports = function(registry) {
213213
this._runWhenAttachedToApp(function(app) {
214214
var remotes = app.remotes();
215215
remotes.after(className + '.' + name, function(ctx, next) {
216-
fn(ctx, ctx.result, next);
216+
return fn(ctx, ctx.result, next);
217217
});
218218
});
219219
};

test/model.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var loopback = require('../');
1010
var ACL = loopback.ACL;
1111
var defineModelTestsWithDataSource = require('./util/model-tests');
1212
var PersistedModel = loopback.PersistedModel;
13+
var Promise = require('bluebird');
1314
var sinonChai = require('sinon-chai');
1415
chai.use(sinonChai);
1516

@@ -308,6 +309,32 @@ describe.onServer('Remote Methods', function() {
308309
done();
309310
});
310311
});
312+
313+
it('Does not stop the hook chain after returning a promise', function(done) {
314+
var hooksCalled = [];
315+
316+
User.beforeRemote('create', function() {
317+
hooksCalled.push('first');
318+
return Promise.resolve();
319+
});
320+
321+
User.beforeRemote('create', function(ctx, user, next) {
322+
hooksCalled.push('second');
323+
next();
324+
});
325+
326+
// invoke save
327+
request(app)
328+
.post('/users')
329+
.send({ data: { first: 'foo', last: 'bar' }})
330+
.expect('Content-Type', /json/)
331+
.expect(200)
332+
.end(function(err, res) {
333+
if (err) return done(err);
334+
expect(hooksCalled).to.eql(['first', 'second']);
335+
done();
336+
});
337+
});
311338
});
312339

313340
describe('Model.afterRemote(name, fn)', function() {

0 commit comments

Comments
 (0)