-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
__use.ts
26 lines (23 loc) · 940 Bytes
/
__use.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
import { wrapPromiseWithState } from "../../../utilities/index.js";
import * as React from "rehackt";
type Use = <T>(promise: Promise<T>) => T;
// Prevent webpack from complaining about our feature detection of the
// use property of the React namespace, which is expected not
// to exist when using current stable versions, and that's fine.
const useKey = "use" as keyof typeof React;
const realHook = React[useKey] as Use | undefined;
// This is named with two underscores to allow this hook to evade typical rules of
// hooks (i.e. it can be used conditionally)
export const __use =
realHook ||
function __use<TValue>(promise: Promise<TValue>) {
const statefulPromise = wrapPromiseWithState(promise);
switch (statefulPromise.status) {
case "pending":
throw statefulPromise;
case "rejected":
throw statefulPromise.reason;
case "fulfilled":
return statefulPromise.value;
}
};