Skip to content

Commit

Permalink
feat(Promise): created Promise never function
Browse files Browse the repository at this point in the history
  • Loading branch information
alisahinozcelik committed Nov 20, 2020
1 parent c33a562 commit 2836770
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 2 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,27 @@ deepest(c, 'x'); // {x: null} (a)

### Promise

#### [Never](https://thalesrc.github.io/js-utils/modules/_promise_never_.html)
A promise which never resolves

```typescript
import { never, NEVER } from '@thalesrc/js-utils/promise';

function foo(promise = never()) {
promise.then(val => {
...
});
}

// or

function foo(promise = NEVER) {
promise.then(val => {
...
});
}
```

#### [Revert](https://thalesrc.github.io/js-utils/modules/_promise_revert_.html)
Exchanges resolve state with rejection of a promise

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"unique id",
"replace",
"remove",
"deepest"
"deepest",
"never"
],
"repository": {
"type": "git",
Expand Down
3 changes: 2 additions & 1 deletion src/promise/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './never';
export * from './revert';
export * from './try-catch';
export * from './timeout';
export * from './revert';
42 changes: 42 additions & 0 deletions src/promise/never.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'jest';

import { never, NEVER } from './never';
import { timeout } from './timeout';

describe('Never Function', () => {
it('should not resolve', done => {
const foo = never();

foo.then(() => {
throw new Error();
}, () => {
throw new Error();
});

timeout(50)
.then(() => {
done();
});
});

it('should return the same `NEVER` instance', () => {
const foo = never();

expect(foo).toBe(NEVER);
});
});

describe('Never Constant', () => {
it('should not resolve', done => {
NEVER.then(() => {
throw new Error();
}, () => {
throw new Error();
});

timeout(50)
.then(() => {
done();
});
});
});
39 changes: 39 additions & 0 deletions src/promise/never.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { noop } from '../function/noop';

/**
* A promise which never resolves
*
* Example:
* ```typescript
* import { NEVER } from '@thalesrc/js-utils/promise';
*
* function foo(promise = NEVER) {
* promise.then(val => {
* ...
* });
* }
* ```
*/
export const NEVER = new Promise(noop);

/**
* Creates a promise which never resolves
*
* Example:
* ```typescript
* import { never } from '@thalesrc/js-utils/promise';
*
* function foo(promise) {
* promise = promise || never();
*
* promise.then(val => {
* ...
* });
* }
* ```
*
* @returns the promise which never resolves
*/
export function never(): Promise<unknown> {
return NEVER;
}
43 changes: 43 additions & 0 deletions src/promise/static/never.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import 'jest';

import './never';

import { timeout } from '../timeout';

describe('Never Function', () => {
it('should not resolve', done => {
const foo = Promise.never();

foo.then(() => {
throw new Error();
}, () => {
throw new Error();
});

timeout(50)
.then(() => {
done();
});
});

it('should return the same `NEVER` instance', () => {
const foo = Promise.never();

expect(foo).toBe(Promise.NEVER);
});
});

describe('Never Constant', () => {
it('should not resolve', done => {
Promise.NEVER.then(() => {
throw new Error();
}, () => {
throw new Error();
});

timeout(50)
.then(() => {
done();
});
});
});
44 changes: 44 additions & 0 deletions src/promise/static/never.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { never, NEVER } from '../never';

declare global {
export interface PromiseConstructor {
/**
* Creates a promise which never resolves
*
* Example:
* ```typescript
* import '@thalesrc/js-utils/promise/static/never';
*
* function foo(promise) {
* promise = promise || Promise.never();
*
* promise.then(val => {
* ...
* });
* }
* ```
*
* @returns the promise which never resolves
*/
never: typeof never;

/**
* A promise which never resolves
*
* Example:
* ```typescript
* import '@thalesrc/js-utils/promise/static/never';
*
* function foo(promise = Promise.NEVER) {
* promise.then(val => {
* ...
* });
* }
* ```
*/
NEVER: typeof NEVER;
}
}

Promise.never = never;
Promise.NEVER = NEVER;

0 comments on commit 2836770

Please sign in to comment.