ant-design/.dumi/hooks/use.ts
MadCcc d1f8b500df
perf: better async component (#43982)
* perf: better async component

* feat: useFetch

* chore: code clean

* chore: code clean

* chore: fix lint

* type: enhance
2023-08-03 14:45:51 +08:00

29 lines
700 B
TypeScript

export default function use<T>(promise: PromiseLike<T>): T {
const internal: PromiseLike<T> & {
status?: 'pending' | 'fulfilled' | 'rejected';
value?: T;
reason?: any;
} = promise;
if (internal.status === 'fulfilled') {
return internal.value;
}
if (internal.status === 'rejected') {
throw internal.reason;
} else if (internal.status === 'pending') {
throw internal;
} else {
internal.status = 'pending';
internal.then(
(result) => {
internal.status = 'fulfilled';
internal.value = result;
},
(reason) => {
internal.status = 'rejected';
internal.reason = reason;
},
);
throw internal;
}
}