Skip to content

Commit 0b26872

Browse files
committed
docs: add documentation for post hooks returning promises
Fix #15
1 parent da58099 commit 0b26872

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,30 @@ hooks.execPost('cook', null, [1, 2], function(error, eggs, bacon) {
243243
});
244244
```
245245

246+
#### It supports returning a promise
247+
248+
You can also return a promise from your post hooks instead of calling
249+
`next()`. When the returned promise resolves, kareem will kick off the
250+
next middleware.
251+
252+
253+
```javascript
254+
hooks.post('cook', function(bacon) {
255+
return new Promise(resolve => {
256+
setTimeout(() => {
257+
this.bacon = 3;
258+
resolve();
259+
}, 100);
260+
});
261+
});
262+
263+
var obj = { bacon: 0 };
264+
265+
hooks.execPost('cook', obj, obj, function() {
266+
assert.equal(obj.bacon, 3);
267+
});
268+
```
269+
246270
## wrap()
247271

248272
acquit:ignore:end

test/examples.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,30 @@ describe('post hooks', function() {
242242
// acquit:ignore:end
243243
});
244244
});
245+
246+
/* You can also return a promise from your post hooks instead of calling
247+
* `next()`. When the returned promise resolves, kareem will kick off the
248+
* next middleware.
249+
*/
250+
it('supports returning a promise', function(done) {
251+
hooks.post('cook', function(bacon) {
252+
return new Promise(resolve => {
253+
setTimeout(() => {
254+
this.bacon = 3;
255+
resolve();
256+
}, 100);
257+
});
258+
});
259+
260+
var obj = { bacon: 0 };
261+
262+
hooks.execPost('cook', obj, obj, function() {
263+
assert.equal(obj.bacon, 3);
264+
// acquit:ignore:start
265+
done();
266+
// acquit:ignore:end
267+
});
268+
});
245269
});
246270

247271
describe('wrap()', function() {

0 commit comments

Comments
 (0)