-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Promise): created Promise never function
- Loading branch information
1 parent
c33a562
commit 2836770
Showing
7 changed files
with
193 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,8 @@ | |
"unique id", | ||
"replace", | ||
"remove", | ||
"deepest" | ||
"deepest", | ||
"never" | ||
], | ||
"repository": { | ||
"type": "git", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |