mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-03 00:09:39 +08:00
d1f8b500df
* perf: better async component * feat: useFetch * chore: code clean * chore: code clean * chore: fix lint * type: enhance
29 lines
700 B
TypeScript
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;
|
|
}
|
|
}
|