-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuse-promise.hook.ts
59 lines (56 loc) · 2.52 KB
/
use-promise.hook.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { useEffect, useState } from 'react';
/**
* Awaits a promise and returns a loading value while the promise is unresolved
*
* @param promiseFactoryCallback A function that returns the promise to await. If the promise
* resolves to undefined, the value will not change. If this callback is undefined, the current
* value will be returned (defaultValue unless it was previously changed and preserveValue is
* true), and there will be no loading.
*
* WARNING: MUST BE STABLE - const or wrapped in useCallback. The reference must not be updated
* every render
* @param defaultValue The initial value to return while first awaiting the promise. If
* preserveValue is false, this value is also shown while awaiting the promise on subsequent
* calls.
*
* WARNING: MUST BE STABLE - const or wrapped in useState, useMemo, etc. The reference must not be
* updated every render
* @param preserveValue Whether to leave the value as the most recent resolved promise value or set
* it back to defaultValue while running the promise again. Default to true
* @returns `[value, isLoading]`
*
* - `value`: the current value for the promise, either the defaultValue or the resolved promise value
* - `isLoading`: whether the promise is waiting to be resolved
*/
const usePromise = <T>(
promiseFactoryCallback: (() => Promise<T | undefined>) | undefined,
defaultValue: T,
preserveValue = true,
): [value: T, isLoading: boolean] => {
const [value, setValue] = useState<T>(() => defaultValue);
const [isLoading, setIsLoading] = useState<boolean>(true);
useEffect(() => {
let promiseIsCurrent = true;
// If a promiseFactoryCallback was provided, we are loading. Otherwise, there is no loading to do
setIsLoading(!!promiseFactoryCallback);
(async () => {
// If there is a callback to run, run it
if (promiseFactoryCallback) {
const result = await promiseFactoryCallback();
// If the promise was not already replaced, update the value
if (promiseIsCurrent) {
// If the promise returned undefined, it purposely did this to do nothing. Maybe its dependencies are not set up
if (result !== undefined) setValue(() => result);
setIsLoading(false);
}
}
})();
return () => {
// Mark this promise as old and not to be used
promiseIsCurrent = false;
if (!preserveValue) setValue(() => defaultValue);
};
}, [promiseFactoryCallback, defaultValue, preserveValue]);
return [value, isLoading];
};
export default usePromise;