Skip to content

Commit

Permalink
Build: Add eslint and jscs presets & update code
Browse files Browse the repository at this point in the history
  • Loading branch information
pdehaan authored and phated committed May 17, 2016
1 parent e1d3945 commit 3e8e9fd
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 119 deletions.
13 changes: 1 addition & 12 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
{
"rules": {
"semi": [2, "always"],
"quotes": [2, "single"],
"strict": [2, "global"],
"no-underscore-dangle": 0,
"no-use-before-define": 0
},
"env": {
"es6": true,
"browser": true,
"node": true
}
"extends": "gulp"
}
3 changes: 3 additions & 0 deletions .jscsrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"preset": "gulp"
}
25 changes: 0 additions & 25 deletions .jshintrc

This file was deleted.

4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
sudo: false

language: node_js

node_js:
- '0.10'
- '0.12'
- 'iojs'
- 'stable'
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
async-done
==========

[![build status](https://secure.travis-ci.org/phated/async-done.png)](http://travis-ci.org/phated/async-done)
[![build status](https://secure.travis-ci.org/gulpjs/async-done.png)](http://travis-ci.org/gulpjs/async-done)

Handles completion and errors for callbacks, promises, observables, child processes and streams.

Expand Down
24 changes: 12 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,31 @@ var once = require('once');
var exhaust = require('stream-exhaust');

var eosConfig = {
error: false
error: false,
};

function asyncDone(fn, cb){
function asyncDone(fn, cb) {
cb = once(cb);

var d = domain.create();
d.once('error', onError);
var domainBoundFn = d.bind(fn);

function done(){
function done() {
d.removeListener('error', onError);
d.exit();
return cb.apply(null, arguments);
}

function onSuccess(result){
function onSuccess(result) {
return done(null, result);
}

function onError(error){
function onError(error) {
return done(error);
}

function asyncRunner(){
function asyncRunner() {
var result = domainBoundFn(done);

function onNext(state) {
Expand All @@ -43,21 +43,21 @@ function asyncDone(fn, cb){
return onSuccess(onNext.state);
}

if(result && typeof result.on === 'function'){
// assume node stream
if (result && typeof result.on === 'function') {
// Assume node stream
d.add(result);
eos(exhaust(result), eosConfig, done);
return;
}

if(result && typeof result.subscribe === 'function'){
// assume RxJS observable
if (result && typeof result.subscribe === 'function') {
// Assume RxJS observable
result.subscribe(onNext, onError, onCompleted);
return;
}

if(result && typeof result.then === 'function'){
// assume promise
if (result && typeof result.then === 'function') {
// Assume promise
result.then(onSuccess, onError);
return;
}
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"Pawel Kozlowski <pkozlowski.opensource@gmail.com>",
"Matthew Podwysocki <matthew.podwysocki@gmail.com>"
],
"repository": "phated/async-done",
"repository": "gulpjs/async-done",
"license": "MIT",
"engines": {
"node": ">= 0.10"
Expand All @@ -19,7 +19,8 @@
"LICENSE"
],
"scripts": {
"test": "lab -cvL"
"lint": "eslint . && jscs *.js test/",
"test": "lab -cv"
},
"dependencies": {
"end-of-stream": "^1.1.0",
Expand All @@ -29,6 +30,10 @@
},
"devDependencies": {
"code": "^1.4.1",
"eslint": "^1.7.3",
"eslint-config-gulp": "^2.0.0",
"jscs": "^2.3.5",
"jscs-preset-gulp": "^1.0.0",
"lab": "^6.2.0",
"rx": "^4.0.6",
"through2": "^2.0.0",
Expand Down
3 changes: 3 additions & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "gulp/test"
}
8 changes: 4 additions & 4 deletions test/arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ var expect = require('code').expect;

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

function twoArg(cb){
function twoArg(cb) {
cb(null, 1, 2);
}

describe('arguments', function(){
describe('arguments', function() {

it('passes all arguments to the completion callback', function(done){
asyncDone(twoArg, function(err, arg1, arg2){
it('passes all arguments to the completion callback', function(done) {
asyncDone(twoArg, function(err, arg1, arg2) {
expect(arg1).to.equal(1);
expect(arg2).to.equal(2);
done(err);
Expand Down
30 changes: 15 additions & 15 deletions test/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,53 @@ var expect = require('code').expect;

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

function success(cb){
function success(cb) {
cb(null, 2);
}

function failure(cb){
function failure(cb) {
cb(new Error('Callback Error'));
}

function neverDone(){
function neverDone() {
return 2;
}

describe('callbacks', function(){
describe('callbacks', function() {

it('should handle a successful callback', function(done){
asyncDone(success, function(err, result){
it('should handle a successful callback', function(done) {
asyncDone(success, function(err, result) {
expect(result).to.equal(2);
done(err);
});
});

it('should handle an errored callback', function(done){
asyncDone(failure, function(err){
it('should handle an errored callback', function(done) {
asyncDone(failure, function(err) {
expect(err).to.be.instanceof(Error);
done();
});
});

it('a function that takes an argument but never calls callback', function(done){
asyncDone(neverDone, function(){
it('a function that takes an argument but never calls callback', function(done) {
asyncDone(neverDone, function() {
done(new Error('Callback called'));
});

setTimeout(function(){
setTimeout(function() {
done();
}, 1000);
});

it('should not handle error if something throws inside the callback', function(done){
it('should not handle error if something throws inside the callback', function(done) {
var d = require('domain').create();
d.on('error', function(err){
d.on('error', function(err) {
expect(err).to.be.instanceof(Error);
done();
});

d.run(function(){
asyncDone(success, function(){
d.run(function() {
asyncDone(success, function() {
throw new Error('Thrown Error');
});
});
Expand Down
26 changes: 13 additions & 13 deletions test/child_processes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,46 @@ var cp = require('child_process');
var asyncDone = require('../');


function execSuccess(){
function execSuccess() {
return cp.exec('echo hello world');
}

function execFail(){
function execFail() {
return cp.exec('foo-bar-baz hello world');
}

function spawnSuccess(){
function spawnSuccess() {
return cp.spawn('echo', ['hello world']);
}

function spawnFail(){
function spawnFail() {
return cp.spawn('foo-bar-baz', ['hello world']);
}

describe('child processes', function(){
it('should handle successful exec', function(done){
asyncDone(execSuccess, function(err){
describe('child processes', function() {
it('should handle successful exec', function(done) {
asyncDone(execSuccess, function(err) {
expect(err).to.not.be.instanceof(Error);
done();
});
});

it('should handle failing exec', function(done){
asyncDone(execFail, function(err){
it('should handle failing exec', function(done) {
asyncDone(execFail, function(err) {
expect(err).to.be.an.instanceof(Error);
done();
});
});

it('should handle successful spawn', function(done){
asyncDone(spawnSuccess, function(err){
it('should handle successful spawn', function(done) {
asyncDone(spawnSuccess, function(err) {
expect(err).to.not.be.instanceof(Error);
done();
});
});

it('should handle failing spawn', function(done){
asyncDone(spawnFail, function(err){
it('should handle failing spawn', function(done) {
asyncDone(spawnFail, function(err) {
expect(err).to.be.an.instanceof(Error);
done();
});
Expand Down
20 changes: 10 additions & 10 deletions test/observables.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,36 @@ var asyncDone = require('../');

var Observable = require('rx').Observable;

function success(){
function success() {
return Observable.empty();
}

function successValue(){
function successValue() {
return Observable.return(42);
}

function failure(){
function failure() {
return Observable.throw(new Error('Observable error'));
}

describe('observables', function(){
describe('observables', function() {

it('should handle a finished observable', function(done){
asyncDone(success, function(err, result){
it('should handle a finished observable', function(done) {
asyncDone(success, function(err, result) {
expect(result).to.equal(undefined);
done(err);
});
});

it('should handle a finished observable with value', function(done){
asyncDone(successValue, function(err, result){
it('should handle a finished observable with value', function(done) {
asyncDone(successValue, function(err, result) {
expect(result).to.equal(42);
done(err);
});
});

it('should handle an errored observable', function(done){
asyncDone(failure, function(err){
it('should handle an errored observable', function(done) {
asyncDone(failure, function(err) {
expect(err).to.be.instanceof(Error);
done();
});
Expand Down
14 changes: 7 additions & 7 deletions test/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ var when = require('when');

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

function success(){
function success() {
return when.resolve(2);
}

function failure(){
function failure() {
return when.reject(new Error('Promise Error'));
}

describe('promises', function(){
describe('promises', function() {

it('should handle a resolved promise', function(done){
asyncDone(success, function(err, result){
it('should handle a resolved promise', function(done) {
asyncDone(success, function(err, result) {
expect(result).to.equal(2);
done(err);
});
});

it('should handle a rejected promise', function(done){
asyncDone(failure, function(err){
it('should handle a rejected promise', function(done) {
asyncDone(failure, function(err) {
expect(err).to.be.instanceof(Error);
done();
});
Expand Down
Loading

0 comments on commit 3e8e9fd

Please sign in to comment.