Skip to content

Commit

Permalink
chore: Run prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
phated authored and actions-user committed Nov 22, 2021
1 parent 66f987f commit b6612f5
Show file tree
Hide file tree
Showing 15 changed files with 162 additions and 155 deletions.
60 changes: 33 additions & 27 deletions README.md
Expand Up @@ -10,7 +10,7 @@

Allows libraries to handle various caller provided asynchronous functions uniformly. Maps promises, observables, child processes and streams, and callbacks to callback style.

As async conventions evolve, it is useful to be able to deal with several different *styles* of async completion uniformly. With this module you can handle completion using a node-style callback, regardless of a return value that's a promise, observable, child process or stream.
As async conventions evolve, it is useful to be able to deal with several different _styles_ of async completion uniformly. With this module you can handle completion using a node-style callback, regardless of a return value that's a promise, observable, child process or stream.

## Usage

Expand All @@ -19,27 +19,33 @@ As async conventions evolve, it is useful to be able to deal with several differ
```js
var asyncDone = require('async-done');

asyncDone(function(done){
// do async things
done(null, 2);
}, function(error, result){
// `error` will be null on successful execution of the first function.
// `result` will be the result from the first function.
});
asyncDone(
function (done) {
// do async things
done(null, 2);
},
function (error, result) {
// `error` will be null on successful execution of the first function.
// `result` will be the result from the first function.
}
);
```

### Failed completion

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

asyncDone(function(done){
// do async things
done(new Error('Some Error Occurred'));
}, function(error, result){
// `error` will be an error from the first function.
// `result` will be undefined on failed execution of the first function.
});
asyncDone(
function (done) {
// do async things
done(new Error('Some Error Occurred'));
},
function (error, result) {
// `error` will be an error from the first function.
// `result` will be undefined on failed execution of the first function.
}
);
```

## API
Expand All @@ -54,24 +60,24 @@ Optionally takes a callback to call when async tasks are complete.

#### Completion and Error Resolution

* `Callback` (`done`) called
- `Callback` (`done`) called
- Completion: called with null error
- Error: called with non-null error
* `Stream` or `EventEmitter` returned
- `Stream` or `EventEmitter` returned
- Completion: [end-of-stream][end-of-stream] module
- Error: [domains][domains]
- __Note:__ Only actual streams are supported, not faux-streams; Therefore, modules like [`event-stream`][event-stream] are not supported.
* `Child Process` returned
- **Note:** Only actual streams are supported, not faux-streams; Therefore, modules like [`event-stream`][event-stream] are not supported.
- `Child Process` returned
- Completion [end-of-stream][end-of-stream] module
- Error: [domains][domains]
* `Promise` returned
- `Promise` returned
- Completion: [onFulfilled][promise-onfulfilled] method called
- Error: [onRejected][promise-onrejected] method called
* `Observable` (e.g. from [RxJS v5][rxjs5-observable] or [RxJS v4][rxjs4-observable]) returned
- `Observable` (e.g. from [RxJS v5][rxjs5-observable] or [RxJS v4][rxjs4-observable]) returned
- Completion: [complete][rxjs5-observer-complete] method called
- Error: [error][rxjs5-observer-error] method called

__Warning:__ Sync tasks are __not supported__ and your function will never complete if the one of the above strategies is not used to signal completion. However, thrown errors will be caught by the domain.
**Warning:** Sync tasks are **not supported** and your function will never complete if the one of the above strategies is not used to signal completion. However, thrown errors will be caught by the domain.

#### `callback(error, result)`

Expand All @@ -81,11 +87,11 @@ If an error occurred in the execution of the `fn` function, The `callback` metho

Errors can be caused by:

* A thrown error
* An error passed to a `done` callback
* An `error` event emitted on a returned `Stream`, `EventEmitter` or `Child Process`
* A rejection of a returned `Promise` - If the `Promise` is not rejected with a value, we generate a new `Error`
* The `onError` handler being called on an `Observable`
- A thrown error
- An error passed to a `done` callback
- An `error` event emitted on a returned `Stream`, `EventEmitter` or `Child Process`
- A rejection of a returned `Promise` - If the `Promise` is not rejected with a value, we generate a new `Error`
- The `onError` handler being called on an `Observable`

## License

Expand Down
27 changes: 19 additions & 8 deletions index.d.ts
Expand Up @@ -48,12 +48,11 @@
* }
* ```
*/
import { ChildProcess } from "child_process";
import { EventEmitter } from "events";
import { Stream } from "stream";
import { ChildProcess } from 'child_process';
import { EventEmitter } from 'events';
import { Stream } from 'stream';

declare namespace asyncDone {

/**
* Represents a callback function used to signal the completion of a
* task without any result value.
Expand All @@ -78,16 +77,25 @@ declare namespace asyncDone {
* @see https://github.com/ReactiveX/rxjs/blob/c3c56867eaf93f302ac7cd588034c7d8712f2834/src/internal/Observable.ts#L77
*/
interface Observable<T = any> {
subscribe(next?: (value: T) => void, error?: (error: any) => void, complete?: () => void): any;
subscribe(
next?: (value: T) => void,
error?: (error: any) => void,
complete?: () => void
): any;
}

/**
* Represents an async operation.
*/
export type AsyncTask<R = any> =
((done: VoidCallback) => void)
| ((done: VoidCallback) => void)
| ((done: Callback<R>) => void)
| (() => ChildProcess | EventEmitter | Observable<R> | PromiseLike<R> | Stream);
| (() =>
| ChildProcess
| EventEmitter
| Observable<R>
| PromiseLike<R>
| Stream);
}

/**
Expand All @@ -96,6 +104,9 @@ declare namespace asyncDone {
* @param fn Function to execute.
* @param callback Function to call on completion.
*/
declare function asyncDone<R = any>(fn: asyncDone.AsyncTask<R>, callback: asyncDone.Callback<R>): void;
declare function asyncDone<R = any>(
fn: asyncDone.AsyncTask<R>,
callback: asyncDone.Callback<R>
): void;

export = asyncDone;
7 changes: 3 additions & 4 deletions test/arguments.js
Expand Up @@ -8,10 +8,9 @@ function twoArg(cb) {
cb(null, 1, 2);
}

describe('arguments', function() {

it('passes all arguments to the completion callback', function(done) {
asyncDone(twoArg, function(err, arg1, arg2) {
describe('arguments', function () {
it('passes all arguments to the completion callback', function (done) {
asyncDone(twoArg, function (err, arg1, arg2) {
expect(arg1).toEqual(1);
expect(arg2).toEqual(2);
done(err);
Expand Down
25 changes: 12 additions & 13 deletions test/callbacks.js
Expand Up @@ -16,41 +16,40 @@ function neverDone() {
return 2;
}

describe('callbacks', function() {

it('should handle a successful callback', function(done) {
asyncDone(success, function(err, result) {
describe('callbacks', function () {
it('should handle a successful callback', function (done) {
asyncDone(success, function (err, result) {
expect(result).toEqual(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).toBeInstanceOf(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).toBeInstanceOf(Error);
done();
});

d.run(function() {
asyncDone(success, function() {
d.run(function () {
asyncDone(success, function () {
throw new Error('Thrown Error');
});
});
Expand Down
19 changes: 9 additions & 10 deletions test/child_processes.js
Expand Up @@ -5,7 +5,6 @@ var expect = require('expect');
var cp = require('child_process');
var asyncDone = require('../');


function execSuccess() {
return cp.exec('echo hello world');
}
Expand All @@ -22,30 +21,30 @@ 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).not.toBeInstanceOf(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).toBeInstanceOf(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).not.toBeInstanceOf(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).toBeInstanceOf(Error);
done();
});
Expand Down
15 changes: 7 additions & 8 deletions test/observables.js
Expand Up @@ -19,24 +19,23 @@ function failure() {
return rxjs.throw(new Error('Observable error'));
}

describe('observables', function() {

it('should handle a finished observable', function(done) {
asyncDone(success, function(err, result) {
describe('observables', function () {
it('should handle a finished observable', function (done) {
asyncDone(success, function (err, result) {
expect(result).toBeUndefined();
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).toEqual(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).toBeInstanceOf(Error);
done();
});
Expand Down
23 changes: 11 additions & 12 deletions test/promises.js
Expand Up @@ -18,39 +18,38 @@ function rejectNoError() {
return Promise.reject();
}

describe('promises', function() {

it('should handle a resolved promise', function(done) {
asyncDone(success, function(err, result) {
describe('promises', function () {
it('should handle a resolved promise', function (done) {
asyncDone(success, function (err, result) {
expect(result).toEqual(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).toBeInstanceOf(Error);
done();
});
});

it('properly errors when rejected without an error', function(done) {
asyncDone(rejectNoError, function(err) {
it('properly errors when rejected without an error', function (done) {
asyncDone(rejectNoError, function (err) {
expect(err).toBeTruthy();
expect(err).toBeInstanceOf(Error);
done();
});
});

it('does not swallow thrown errors in callback', function(done) {
it('does not swallow thrown errors in callback', function (done) {
var d = domain.create();
d.once('error', function(err) {
d.once('error', function (err) {
expect(err).toBeTruthy();
expect(err.message).toContain('Boom');
done();
});
d.run(function() {
asyncDone(success, function() {
d.run(function () {
asyncDone(success, function () {
throw new Error('Boom');
});
});
Expand Down

0 comments on commit b6612f5

Please sign in to comment.