Skip to content

Commit c58bff6

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 f9a5808 commit c58bff6

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

lib/model.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ module.exports = function(registry) {
197197
this._runWhenAttachedToApp(function(app) {
198198
var remotes = app.remotes();
199199
remotes.before(className + '.' + name, function(ctx, next) {
200-
fn(ctx, ctx.result, next);
200+
return fn(ctx, ctx.result, next);
201201
});
202202
});
203203
};
@@ -208,7 +208,7 @@ module.exports = function(registry) {
208208
this._runWhenAttachedToApp(function(app) {
209209
var remotes = app.remotes();
210210
remotes.after(className + '.' + name, function(ctx, next) {
211-
fn(ctx, ctx.result, next);
211+
return fn(ctx, ctx.result, next);
212212
});
213213
});
214214
};

test/model.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,32 @@ describe.onServer('Remote Methods', function() {
301301
done();
302302
});
303303
});
304+
305+
it('Does not stop the hook chain after returning a promise', function(done) {
306+
var hooksCalled = [];
307+
308+
User.beforeRemote('create', function() {
309+
hooksCalled.push('first');
310+
return Promise.resolve();
311+
});
312+
313+
User.beforeRemote('create', function(ctx, user, next) {
314+
hooksCalled.push('second');
315+
next();
316+
});
317+
318+
// invoke save
319+
request(app)
320+
.post('/users')
321+
.send({ data: { first: 'foo', last: 'bar' }})
322+
.expect('Content-Type', /json/)
323+
.expect(200)
324+
.end(function(err, res) {
325+
if (err) return done(err);
326+
expect(hooksCalled).to.eql(['first', 'second']);
327+
done();
328+
});
329+
});
304330
});
305331

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

0 commit comments

Comments
 (0)