Merge pull request #2820 from strongloop/fix/promise-remote-hooks
Fix support for remote hooks returning a Promise
This commit is contained in:
commit
4e66009963
|
@ -197,7 +197,7 @@ module.exports = function(registry) {
|
||||||
this._runWhenAttachedToApp(function(app) {
|
this._runWhenAttachedToApp(function(app) {
|
||||||
var remotes = app.remotes();
|
var remotes = app.remotes();
|
||||||
remotes.before(className + '.' + name, function(ctx, next) {
|
remotes.before(className + '.' + name, function(ctx, next) {
|
||||||
fn(ctx, ctx.result, next);
|
return fn(ctx, ctx.result, next);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -208,7 +208,7 @@ module.exports = function(registry) {
|
||||||
this._runWhenAttachedToApp(function(app) {
|
this._runWhenAttachedToApp(function(app) {
|
||||||
var remotes = app.remotes();
|
var remotes = app.remotes();
|
||||||
remotes.after(className + '.' + name, function(ctx, next) {
|
remotes.after(className + '.' + name, function(ctx, next) {
|
||||||
fn(ctx, ctx.result, next);
|
return fn(ctx, ctx.result, next);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
|
@ -301,6 +301,32 @@ describe.onServer('Remote Methods', function() {
|
||||||
done();
|
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() {
|
describe('Model.afterRemote(name, fn)', function() {
|
||||||
|
|
Loading…
Reference in New Issue