@aercolino/wrap-promise v0.1.1
Wrap Promise

Wrap a promise within before and after callbacks.
Installation
$ npm install @aercolino/wrap-promiseThis is not related to wrap-promise.
Usage
const { $wrapPromise } = require('@aercolino/wrap-promise');
const regular = $wrapPromise($promise, before, after);
const special = $wrapPromise($promise, before, afterFulfillment, afterRejection);$promiseandbeforeare called with no arguments.In the regular case:
afteris called with the fulfillment{ value }or the rejection{ reason }.no matter whether executing the fulfillment line or the rejection line,
afteris called in both cases, without changing line, without changing the fulfillment value nor the rejection reason. (i.e.afterreturn value is discarded)
In the special case:
afterFulfillmentis called with the fulfillment{ value }.afterRejectionis called with the rejection{ reason }.afterFulfillmentis called only if executing the fulfillment line, without changing line, without changing the fulfillment value.afterRejectionis called only if executing the rejection line, by default changing to the fulfillment line (i.e. swallowing the rejection) and makingafterRejectionreturn value the new fullfilment value.If you prefer to stay on the rejection line, just
throwfrom insideafterRejection, as usual.
Example
// test.js
require('console.mute');
const { $wrapPromise } = require('@aercolino/wrap-promise');
function muted($fn, ...args) {
return $wrapPromise(
() => $fn(...args),
() => {
console.mute();
},
() => {
console.resume();
},
);
}
...
it('should be quiet when calling this'
...
it('should quietly run that noisy function too', function() {
return muted(noisy, 'bye bye', 'clutter')
.then(() => {
expect(that).to.have.been.called;
});
});
it('should be quiet when calling that'
...
...// console
$ npm test
...
✓ should be quiet when calling this
✓ should quietly run that noisy function too
✓ should be quiet when calling that
...
5 passing (64ms)Tests
$ npm test