Skip to content

Commit bf947c3

Browse files
committed
feat: rework the module.
Now only undefined rejection is treated as 'empty value' and replaced with Error instance. Fixed types as requested in #9 & #15 not return type is inferred from promise. BREAKING CHANGE: now module has default export only.
1 parent 5b90a6c commit bf947c3

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

src/index.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
export type AWAIT_OF_RETURN_TYPE = [any?, Error?];
1+
type AwaitOfReturnType<T> = Readonly<[T | undefined, Error?]>;
22

3-
export function of(promise: Promise<any>): Promise<AWAIT_OF_RETURN_TYPE> {
3+
export default function of<T = any>(promise: Promise<T>): Promise<AwaitOfReturnType<T>> {
44
return Promise.resolve(promise)
5-
.then((r: any): AWAIT_OF_RETURN_TYPE => [r])
5+
.then((result): Readonly<[T]> => [result])
66
.catch(
7-
(e: Error | undefined | null): AWAIT_OF_RETURN_TYPE => {
8-
if (e === undefined || e === null) {
9-
return [e, new Error('Rejection with empty value')];
7+
(err): Readonly<[undefined, Error]> => {
8+
if (typeof err === 'undefined') {
9+
err = new Error('Rejection with empty value');
1010
}
1111

12-
return [undefined, e];
12+
return [undefined, err];
1313
},
1414
);
1515
}
16-
17-
export default of;

tests/index.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-non-null-assertion,@typescript-eslint/ban-ts-ignore */
2-
import { of } from '../src';
2+
import of from '../src';
33

44
describe('await-of', () => {
55
it('should return resolve result as first array`s item', async () => {
@@ -18,7 +18,7 @@ describe('await-of', () => {
1818
});
1919

2020
it('should return unhandled error as second array`s item', async () => {
21-
const [res, err = undefined] = await of(
21+
const [res, err] = await of(
2222
new Promise(() => {
2323
throw new Error('Unhandled error!');
2424
}),

0 commit comments

Comments
 (0)