Skip to content

Commit

Permalink
Merge pull request #363 from strongloop/fix/promise-hooks
Browse files Browse the repository at this point in the history
Fix support for hooks returning a Promise
  • Loading branch information
bajtos authored Oct 4, 2016
2 parents 0f7b264 + 39a75c8 commit dc6e8c7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/remote-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ RemoteObjects.prototype.execHooks = function(when, method, scope, ctx, next) {
try {
var result = cur.call(scope, ctx, execStack, method);
if (result && typeof result.then === 'function') {
result.then(function() { next(); }, next);
result.then(function() { execStack(); }, next);
}
} catch (err) {
next(err);
Expand Down
16 changes: 12 additions & 4 deletions test/rest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1905,13 +1905,21 @@ describe('strong-remoting-rest', function() {

it('should resolve promise returned by a hook', function(done) {
var method = givenSharedPrototypeMethod();
var hooksCalled = [];
objects.before('**', function(ctx) {
return new Promise(function(resolve, reject) {
resolve('value-to-ignore');
});
hooksCalled.push('first');
return Promise.resolve();
});
objects.before('**', function(ctx) {
hooksCalled.push('second');
return Promise.resolve();
});

json(method.url).expect(204).end(done);
json(method.url).expect(204, function(err, res) {
if (err) return done(err);
expect(hooksCalled).to.eql(['first', 'second']);
return done();
});
});

it('should handle rejected promise returned by a hook', function(done) {
Expand Down

0 comments on commit dc6e8c7

Please sign in to comment.