Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
57 additions
and
6 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
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,33 @@ | ||
import { isPromiseLike } from './is-promise-like'; | ||
|
||
describe('isPromiseLike()', () => { | ||
function getArguments(): IArguments { | ||
return arguments; | ||
} | ||
|
||
it('works', () => { | ||
expect(isPromiseLike(Promise.resolve('hi'))).toBe(true); | ||
expect(isPromiseLike(Promise.reject('bye'))).toBe(true); | ||
|
||
// falsey values | ||
expect(isPromiseLike(undefined)).toBe(false); | ||
expect(isPromiseLike(null)).toBe(false); | ||
expect(isPromiseLike(false)).toBe(false); | ||
expect(isPromiseLike(0)).toBe(false); | ||
expect(isPromiseLike(NaN)).toBe(false); | ||
expect(isPromiseLike('')).toBe(false); | ||
|
||
// all the other things | ||
expect(isPromiseLike(getArguments())).toBe(false); | ||
expect(isPromiseLike([1, 2, 3])).toBe(false); | ||
expect(isPromiseLike(true)).toBe(false); | ||
expect(isPromiseLike(new Date())).toBe(false); | ||
expect(isPromiseLike(new Error())).toBe(false); | ||
expect(isPromiseLike(Array.prototype.slice)).toBe(false); | ||
expect(isPromiseLike({ a: 1 })).toBe(false); | ||
expect(isPromiseLike(1)).toBe(false); | ||
expect(isPromiseLike(/x/)).toBe(false); | ||
expect(isPromiseLike('a')).toBe(false); | ||
expect(isPromiseLike(Symbol('a'))).toBe(false); | ||
}); | ||
}); |
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,13 @@ | ||
/** | ||
* Checks if `value` has a `then()` function, indicating that it is probably promise-like. | ||
* | ||
* ```ts | ||
* isPromiseLike(Promise.resolve('hi')); // true | ||
* isPromiseLike(Promise.reject('bye')); // true | ||
* isPromiseLike({}); // false | ||
* isPromiseLike(null); // false | ||
* ``` | ||
*/ | ||
export function isPromiseLike(value: any): value is PromiseLike<unknown> { | ||
return typeof value?.then === 'function'; | ||
} |
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