Skip to content

Commit 60a12f3

Browse files
authored
feat: add isPromiseLike (#474)
- closed #473
1 parent 049ea98 commit 60a12f3

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export {
110110
isFormData,
111111
isString,
112112
isNumber,
113+
isPromiseLike,
113114
} from './type_checkers';
114115

115116
export type { Dictionary, AnyFunction, SupportEvent, TimeoutHandle, Writeable } from './types';

src/type_checkers.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* eslint-disable @typescript-eslint/no-empty-function */
2+
import { describe, expect, test } from '@jest/globals';
3+
import { isPromiseLike } from './type_checkers';
4+
5+
describe(isPromiseLike, () => {
6+
const promise = { then: function () {} };
7+
8+
const fn = () => {};
9+
fn.then = () => {};
10+
11+
test.each<any>([promise, fn])('isPromiseLike(%s) is true', (a) => {
12+
expect(isPromiseLike(a)).toBeTruthy();
13+
});
14+
15+
test.each<any>([{}, () => {}, { then: true }, [], [true]])('isPromiseLike(%s) is false', (a) => {
16+
expect(isPromiseLike(a)).toBeFalsy();
17+
});
18+
});

src/type_checkers.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ export function isString(object: any): object is string {
2929
export function isNumber(object: any): object is number {
3030
return typeof object === 'number';
3131
}
32+
33+
export function isPromiseLike<T = any>(object: any): object is PromiseLike<T> {
34+
return (isObject(object) || isFunction(object)) && isFunction(object.then);
35+
}

0 commit comments

Comments
 (0)