Skip to content

Commit

Permalink
New: First implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Jun 25, 2016
1 parent 314efb6 commit 7c90a7c
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"node": true,
"strict": true,
"eqnull": true
}
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,25 @@ async-settle
[![build status](https://secure.travis-ci.org/phated/async-settle.png)](http://travis-ci.org/phated/async-settle)

Settle your async functions - when you need to know all your parallel functions are complete (success or failure)

## API

### `settle(executor, onComplete)` : Function

Takes a function to execute (`executor`) and a function to call on completion (`onComplete`).

`executer` is executed in the context of [`async-done`](https://github.com/phated/async-done), with all errors and results being settled.

`onComplete` will be called with a settled value.

#### Settled Values

Settled values have two properties, `state` and `value`.

`state` has two possible options `'error'` and `'success'`.

`value` will be the value passed to original callback.

## License

MIT
21 changes: 21 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

var asyncDone = require('async-done');

function settle(fn, done){
asyncDone(fn, function(error, result){
var settled = {};

if(error != null){
settled.state = 'error';
settled.value = error;
} else {
settled.state = 'success';
settled.value = result;
}

done(undefined, settled);
});
}

module.exports = settle;
36 changes: 36 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "async-settle",
"version": "0.0.0",
"description": "Settle your async functions - when you need to know all your parallel functions are complete (success or failure)",
"main": "index.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "tap ./test"
},
"repository": {
"type": "git",
"url": "git://github.com/phated/async-settle"
},
"keywords": [
"settle",
"async",
"async-done",
"complete",
"error",
"parallel"
],
"author": "Blaine Bublitz <[email protected]> (http://iceddev.com/)",
"license": "MIT",
"bugs": {
"url": "https://github.com/phated/async-settle/issues"
},
"homepage": "https://github.com/phated/async-settle",
"devDependencies": {
"tap": "^0.4.8"
},
"dependencies": {
"async-done": "^0.2.0"
}
}
31 changes: 31 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

var test = require('tap').test;

var settle = require('../');

test('should transform success into settled success values', function(t){
var val = 'value to be settled';
settle(function(done){
done(null, val);
}, function(error, result){
t.notOk(error, 'error should be undefined');
t.ok(result, 'result should be defined');
t.ok(result.state === 'success', 'state should be "success"');
t.equal(result.value, val, 'value should be the success value');
t.end();
});
});

test('should transform errors into settled success values', function(t){
var err = new Error('Error to be settled');
settle(function(done){
done(err);
}, function(error, result){
t.notOk(error, 'error should be undefined');
t.ok(result, 'result should be defined');
t.ok(result.state === 'error', 'state should be "error"');
t.equal(result.value, err, 'value should be the error value');
t.end();
});
});

0 comments on commit 7c90a7c

Please sign in to comment.