Skip to content

Commit

Permalink
src: Remove the Promisify function (#354)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Remove the Promisify function from the CircuitBreaker factory

* Node has its own built-in promisify function that can be used instead.

fixes #352
  • Loading branch information
lholmquist authored and lance committed Jul 12, 2019
1 parent 6bfff7a commit 86a6154
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 38 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,15 @@ circuit.on('fallback',
The `opossum` API returns a `Promise` from `CircuitBreaker.fire()`.
But your circuit action - the async function that might fail -
doesn't have to return a promise. You can easily turn Node.js style
callback functions into something `opossum` understands by using
`circuitBreaker.promisify()`.
callback functions into something `opossum` understands by using the built in
Node core utility function `util.promisify()` .

```javascript
const fs = require('fs');
const { promisify } = require('util');
const circuitBreaker = require('opossum');

const readFile = circuitBreaker.promisify(fs.readFile);
const readFile = promisify(fs.readFile);
const breaker = circuitBreaker(readFile, options);

breaker.fire('./package.json', 'utf-8')
Expand Down
17 changes: 1 addition & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,6 @@ function factory (action, options) {
return lastCircuit;
}

/**
* Given a function that receives a callback as its last argument,
* and which executes that function, passing as parameters `err` and `result`,
* creates an action that returns a promise which resolves when the function's
* callback is executed.
* @function factory.promisify
*
* @param {Function} action A Node.js-like asynchronous function
* @example
* const fs = require('fs');
* const readFilePromised = circuitBreaker.promisify(fs.readFile);
* const breaker = circuitBreaker(readFilePromised);
*/
factory.promisify = require('./lib/promisify');

/**
* Get the Prometheus metrics for all circuits.
* @function factory.metrics
Expand Down Expand Up @@ -115,7 +100,7 @@ Object.defineProperty(factory, 'stats', {
* @return {Iterator} an <code>Iterator</code> of all available circuits
*/
factory.circuits = CircuitBreaker.circuits;

module.exports = exports = factory;
// Allow use of default import syntax in TypeScript
module.exports.default = factory;
15 changes: 0 additions & 15 deletions lib/promisify.js

This file was deleted.

8 changes: 4 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

const browser = require('./browser/browser-tap');
const test = require('tape');
const {promisify} = require('util');
const circuit = require('../');

browser.enable();

test('api', t => {
const breaker = circuit(passFail);
t.ok(breaker, 'CircuitBreaker');
t.ok(circuit.promisify, 'CircuitBreaker.promisify');
t.ok(breaker.fire, 'CircuitBreaker.fire');
t.notOk(breaker.opened, 'CircuitBreaker.opened');
t.notOk(breaker.halfOpen, 'CircuitBreaker.halfOpen');
Expand Down Expand Up @@ -167,19 +167,19 @@ test('Works with non-functions', t => {

test('Works with callback functions', t => {
t.plan(1);
const promisified = circuit.promisify(callbackFunction);
const promisified = promisify(callbackFunction);
const breaker = circuit(promisified);

breaker.fire(3, 4)
.then(arg => t.equals(arg, 7, 'CircuitBreaker.promisify works'))
.then(arg => t.equals(arg, 7, 'Works with a Promisified Callback'))
.then(_ => breaker.shutdown())
.then(t.end)
.catch(t.fail);
});

test('Works with callback functions that fail', t => {
t.plan(1);
const promisified = circuit.promisify(failedCallbackFunction);
const promisified = promisify(failedCallbackFunction);
const breaker = circuit(promisified);

breaker.fire(3, 4)
Expand Down

0 comments on commit 86a6154

Please sign in to comment.