Skip to content

Commit b6612f5

Browse files
phatedactions-user
authored andcommittedNov 22, 2021
chore: Run prettier
1 parent 66f987f commit b6612f5

15 files changed

+162
-155
lines changed
 

‎README.md

+33-27
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

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

13-
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.
13+
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.
1414

1515
## Usage
1616

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

22-
asyncDone(function(done){
23-
// do async things
24-
done(null, 2);
25-
}, function(error, result){
26-
// `error` will be null on successful execution of the first function.
27-
// `result` will be the result from the first function.
28-
});
22+
asyncDone(
23+
function (done) {
24+
// do async things
25+
done(null, 2);
26+
},
27+
function (error, result) {
28+
// `error` will be null on successful execution of the first function.
29+
// `result` will be the result from the first function.
30+
}
31+
);
2932
```
3033

3134
### Failed completion
3235

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

36-
asyncDone(function(done){
37-
// do async things
38-
done(new Error('Some Error Occurred'));
39-
}, function(error, result){
40-
// `error` will be an error from the first function.
41-
// `result` will be undefined on failed execution of the first function.
42-
});
39+
asyncDone(
40+
function (done) {
41+
// do async things
42+
done(new Error('Some Error Occurred'));
43+
},
44+
function (error, result) {
45+
// `error` will be an error from the first function.
46+
// `result` will be undefined on failed execution of the first function.
47+
}
48+
);
4349
```
4450

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

5561
#### Completion and Error Resolution
5662

57-
* `Callback` (`done`) called
63+
- `Callback` (`done`) called
5864
- Completion: called with null error
5965
- Error: called with non-null error
60-
* `Stream` or `EventEmitter` returned
66+
- `Stream` or `EventEmitter` returned
6167
- Completion: [end-of-stream][end-of-stream] module
6268
- Error: [domains][domains]
63-
- __Note:__ Only actual streams are supported, not faux-streams; Therefore, modules like [`event-stream`][event-stream] are not supported.
64-
* `Child Process` returned
69+
- **Note:** Only actual streams are supported, not faux-streams; Therefore, modules like [`event-stream`][event-stream] are not supported.
70+
- `Child Process` returned
6571
- Completion [end-of-stream][end-of-stream] module
6672
- Error: [domains][domains]
67-
* `Promise` returned
73+
- `Promise` returned
6874
- Completion: [onFulfilled][promise-onfulfilled] method called
6975
- Error: [onRejected][promise-onrejected] method called
70-
* `Observable` (e.g. from [RxJS v5][rxjs5-observable] or [RxJS v4][rxjs4-observable]) returned
76+
- `Observable` (e.g. from [RxJS v5][rxjs5-observable] or [RxJS v4][rxjs4-observable]) returned
7177
- Completion: [complete][rxjs5-observer-complete] method called
7278
- Error: [error][rxjs5-observer-error] method called
7379

74-
__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.
80+
**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.
7581

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

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

8288
Errors can be caused by:
8389

84-
* A thrown error
85-
* An error passed to a `done` callback
86-
* An `error` event emitted on a returned `Stream`, `EventEmitter` or `Child Process`
87-
* A rejection of a returned `Promise` - If the `Promise` is not rejected with a value, we generate a new `Error`
88-
* The `onError` handler being called on an `Observable`
90+
- A thrown error
91+
- An error passed to a `done` callback
92+
- An `error` event emitted on a returned `Stream`, `EventEmitter` or `Child Process`
93+
- A rejection of a returned `Promise` - If the `Promise` is not rejected with a value, we generate a new `Error`
94+
- The `onError` handler being called on an `Observable`
8995

9096
## License
9197

‎index.d.ts

+19-8
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,11 @@
4848
* }
4949
* ```
5050
*/
51-
import { ChildProcess } from "child_process";
52-
import { EventEmitter } from "events";
53-
import { Stream } from "stream";
51+
import { ChildProcess } from 'child_process';
52+
import { EventEmitter } from 'events';
53+
import { Stream } from 'stream';
5454

5555
declare namespace asyncDone {
56-
5756
/**
5857
* Represents a callback function used to signal the completion of a
5958
* task without any result value.
@@ -78,16 +77,25 @@ declare namespace asyncDone {
7877
* @see https://github.com/ReactiveX/rxjs/blob/c3c56867eaf93f302ac7cd588034c7d8712f2834/src/internal/Observable.ts#L77
7978
*/
8079
interface Observable<T = any> {
81-
subscribe(next?: (value: T) => void, error?: (error: any) => void, complete?: () => void): any;
80+
subscribe(
81+
next?: (value: T) => void,
82+
error?: (error: any) => void,
83+
complete?: () => void
84+
): any;
8285
}
8386

8487
/**
8588
* Represents an async operation.
8689
*/
8790
export type AsyncTask<R = any> =
88-
((done: VoidCallback) => void)
91+
| ((done: VoidCallback) => void)
8992
| ((done: Callback<R>) => void)
90-
| (() => ChildProcess | EventEmitter | Observable<R> | PromiseLike<R> | Stream);
93+
| (() =>
94+
| ChildProcess
95+
| EventEmitter
96+
| Observable<R>
97+
| PromiseLike<R>
98+
| Stream);
9199
}
92100

93101
/**
@@ -96,6 +104,9 @@ declare namespace asyncDone {
96104
* @param fn Function to execute.
97105
* @param callback Function to call on completion.
98106
*/
99-
declare function asyncDone<R = any>(fn: asyncDone.AsyncTask<R>, callback: asyncDone.Callback<R>): void;
107+
declare function asyncDone<R = any>(
108+
fn: asyncDone.AsyncTask<R>,
109+
callback: asyncDone.Callback<R>
110+
): void;
100111

101112
export = asyncDone;

‎test/arguments.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ function twoArg(cb) {
88
cb(null, 1, 2);
99
}
1010

11-
describe('arguments', function() {
12-
13-
it('passes all arguments to the completion callback', function(done) {
14-
asyncDone(twoArg, function(err, arg1, arg2) {
11+
describe('arguments', function () {
12+
it('passes all arguments to the completion callback', function (done) {
13+
asyncDone(twoArg, function (err, arg1, arg2) {
1514
expect(arg1).toEqual(1);
1615
expect(arg2).toEqual(2);
1716
done(err);

‎test/callbacks.js

+12-13
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,40 @@ function neverDone() {
1616
return 2;
1717
}
1818

19-
describe('callbacks', function() {
20-
21-
it('should handle a successful callback', function(done) {
22-
asyncDone(success, function(err, result) {
19+
describe('callbacks', function () {
20+
it('should handle a successful callback', function (done) {
21+
asyncDone(success, function (err, result) {
2322
expect(result).toEqual(2);
2423
done(err);
2524
});
2625
});
2726

28-
it('should handle an errored callback', function(done) {
29-
asyncDone(failure, function(err) {
27+
it('should handle an errored callback', function (done) {
28+
asyncDone(failure, function (err) {
3029
expect(err).toBeInstanceOf(Error);
3130
done();
3231
});
3332
});
3433

35-
it('a function that takes an argument but never calls callback', function(done) {
36-
asyncDone(neverDone, function() {
34+
it('a function that takes an argument but never calls callback', function (done) {
35+
asyncDone(neverDone, function () {
3736
done(new Error('Callback called'));
3837
});
3938

40-
setTimeout(function() {
39+
setTimeout(function () {
4140
done();
4241
}, 1000);
4342
});
4443

45-
it('should not handle error if something throws inside the callback', function(done) {
44+
it('should not handle error if something throws inside the callback', function (done) {
4645
var d = require('domain').create();
47-
d.on('error', function(err) {
46+
d.on('error', function (err) {
4847
expect(err).toBeInstanceOf(Error);
4948
done();
5049
});
5150

52-
d.run(function() {
53-
asyncDone(success, function() {
51+
d.run(function () {
52+
asyncDone(success, function () {
5453
throw new Error('Thrown Error');
5554
});
5655
});

‎test/child_processes.js

+9-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ var expect = require('expect');
55
var cp = require('child_process');
66
var asyncDone = require('../');
77

8-
98
function execSuccess() {
109
return cp.exec('echo hello world');
1110
}
@@ -22,30 +21,30 @@ function spawnFail() {
2221
return cp.spawn('foo-bar-baz', ['hello world']);
2322
}
2423

25-
describe('child processes', function() {
26-
it('should handle successful exec', function(done) {
27-
asyncDone(execSuccess, function(err) {
24+
describe('child processes', function () {
25+
it('should handle successful exec', function (done) {
26+
asyncDone(execSuccess, function (err) {
2827
expect(err).not.toBeInstanceOf(Error);
2928
done();
3029
});
3130
});
3231

33-
it('should handle failing exec', function(done) {
34-
asyncDone(execFail, function(err) {
32+
it('should handle failing exec', function (done) {
33+
asyncDone(execFail, function (err) {
3534
expect(err).toBeInstanceOf(Error);
3635
done();
3736
});
3837
});
3938

40-
it('should handle successful spawn', function(done) {
41-
asyncDone(spawnSuccess, function(err) {
39+
it('should handle successful spawn', function (done) {
40+
asyncDone(spawnSuccess, function (err) {
4241
expect(err).not.toBeInstanceOf(Error);
4342
done();
4443
});
4544
});
4645

47-
it('should handle failing spawn', function(done) {
48-
asyncDone(spawnFail, function(err) {
46+
it('should handle failing spawn', function (done) {
47+
asyncDone(spawnFail, function (err) {
4948
expect(err).toBeInstanceOf(Error);
5049
done();
5150
});

‎test/observables.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,23 @@ function failure() {
1919
return rxjs.throw(new Error('Observable error'));
2020
}
2121

22-
describe('observables', function() {
23-
24-
it('should handle a finished observable', function(done) {
25-
asyncDone(success, function(err, result) {
22+
describe('observables', function () {
23+
it('should handle a finished observable', function (done) {
24+
asyncDone(success, function (err, result) {
2625
expect(result).toBeUndefined();
2726
done(err);
2827
});
2928
});
3029

31-
it('should handle a finished observable with value', function(done) {
32-
asyncDone(successValue, function(err, result) {
30+
it('should handle a finished observable with value', function (done) {
31+
asyncDone(successValue, function (err, result) {
3332
expect(result).toEqual(42);
3433
done(err);
3534
});
3635
});
3736

38-
it('should handle an errored observable', function(done) {
39-
asyncDone(failure, function(err) {
37+
it('should handle an errored observable', function (done) {
38+
asyncDone(failure, function (err) {
4039
expect(err).toBeInstanceOf(Error);
4140
done();
4241
});

‎test/promises.js

+11-12
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,38 @@ function rejectNoError() {
1818
return Promise.reject();
1919
}
2020

21-
describe('promises', function() {
22-
23-
it('should handle a resolved promise', function(done) {
24-
asyncDone(success, function(err, result) {
21+
describe('promises', function () {
22+
it('should handle a resolved promise', function (done) {
23+
asyncDone(success, function (err, result) {
2524
expect(result).toEqual(2);
2625
done(err);
2726
});
2827
});
2928

30-
it('should handle a rejected promise', function(done) {
31-
asyncDone(failure, function(err) {
29+
it('should handle a rejected promise', function (done) {
30+
asyncDone(failure, function (err) {
3231
expect(err).toBeInstanceOf(Error);
3332
done();
3433
});
3534
});
3635

37-
it('properly errors when rejected without an error', function(done) {
38-
asyncDone(rejectNoError, function(err) {
36+
it('properly errors when rejected without an error', function (done) {
37+
asyncDone(rejectNoError, function (err) {
3938
expect(err).toBeTruthy();
4039
expect(err).toBeInstanceOf(Error);
4140
done();
4241
});
4342
});
4443

45-
it('does not swallow thrown errors in callback', function(done) {
44+
it('does not swallow thrown errors in callback', function (done) {
4645
var d = domain.create();
47-
d.once('error', function(err) {
46+
d.once('error', function (err) {
4847
expect(err).toBeTruthy();
4948
expect(err.message).toContain('Boom');
5049
done();
5150
});
52-
d.run(function() {
53-
asyncDone(success, function() {
51+
d.run(function () {
52+
asyncDone(success, function () {
5453
throw new Error('Boom');
5554
});
5655
});

0 commit comments

Comments
 (0)
Please sign in to comment.