Skip to content

Commit cfb6f28

Browse files
fix: correctly check for promise in useLoadData (#39)
* fix: correctly check for promise in useLoadData * simplification * simplification * add unit test to ensure useLoadData handles thenable objects as promises * improved comment
1 parent 093606b commit cfb6f28

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

hooks/useLoadData/useLoadData.test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -442,4 +442,13 @@ describe('useLoadData', () => {
442442
expect(getSuccess).toHaveBeenCalledTimes(2);
443443
expect(getSuccess).toHaveBeenCalledWith('b');
444444
});
445+
446+
it('should treat a thenable object as a Promise', async () => {
447+
const getThenableSuccess = jest.fn(() => ({then: (resolve: any) => resolve(successResult)}));
448+
449+
const {result} = renderHook(() => useLoadData(getThenableSuccess));
450+
expect(result.current.isInProgress).toBe(true);
451+
await waitFor(() => expect(result.current.isInProgress).toBe(false));
452+
expect(result.current.result).toBe(successResult);
453+
});
445454
});

hooks/useLoadData/useLoadData.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {useEffect, useState, useMemo} from 'react';
2-
import {ApiResponse, RetryResponse, ApiResponseBase, OptionalDependency, DependencyBase} from '../../types';
2+
import {ApiResponse, RetryResponse, ApiResponseBase, OptionalDependency, DependencyBase, Promisable} from '../../types';
33

44
import {FetchData, NotUndefined} from './types';
55

@@ -30,6 +30,15 @@ function unboxApiResponse<T>(arg: ApiResponse<T> | T): T {
3030
}
3131
}
3232

33+
function isPromise<T>(promisable: Promisable<T>): promisable is Promise<T> {
34+
/*
35+
Simply checking promisable instanceof Promise is not sufficient.
36+
Certain environments do not use native promises. Checking for promisable
37+
to be thenable is a more comprehensive and conclusive test.
38+
*/
39+
return promisable && typeof promisable === 'object' && 'then' in promisable && typeof promisable.then === 'function';
40+
}
41+
3342
export interface LoadDataConfig {
3443
fetchWhenDepsChange?: boolean;
3544
maxRetryCount?: number;
@@ -184,7 +193,7 @@ export function useLoadData<T extends NotUndefined, Deps extends any[]>(
184193
}
185194
}, [counter, localFetchWhenDepsChange]);
186195

187-
const nonPromiseResult = initialPromise.res instanceof Promise ? undefined : initialPromise.res;
196+
const nonPromiseResult = isPromise(initialPromise.res) ? undefined : initialPromise.res;
188197
const initialData = data || nonPromiseResult;
189198

190199
// Initialize our pending data to one of three possible states:

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@optum/react-hooks",
3-
"version": "1.0.4",
3+
"version": "1.0.5-next.1",
44
"description": "A reusable set of React hooks",
55
"repository": "https://github.com/Optum/react-hooks",
66
"license": "Apache 2.0",

0 commit comments

Comments
 (0)