|
1 | 1 | 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'; |
3 | 3 |
|
4 | 4 | import {FetchData, NotUndefined} from './types';
|
5 | 5 |
|
@@ -30,6 +30,15 @@ function unboxApiResponse<T>(arg: ApiResponse<T> | T): T {
|
30 | 30 | }
|
31 | 31 | }
|
32 | 32 |
|
| 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 | + |
33 | 42 | export interface LoadDataConfig {
|
34 | 43 | fetchWhenDepsChange?: boolean;
|
35 | 44 | maxRetryCount?: number;
|
@@ -184,7 +193,7 @@ export function useLoadData<T extends NotUndefined, Deps extends any[]>(
|
184 | 193 | }
|
185 | 194 | }, [counter, localFetchWhenDepsChange]);
|
186 | 195 |
|
187 |
| - const nonPromiseResult = initialPromise.res instanceof Promise ? undefined : initialPromise.res; |
| 196 | + const nonPromiseResult = isPromise(initialPromise.res) ? undefined : initialPromise.res; |
188 | 197 | const initialData = data || nonPromiseResult;
|
189 | 198 |
|
190 | 199 | // Initialize our pending data to one of three possible states:
|
|
0 commit comments