ant-design/.dumi/hooks/use.ts

31 lines
673 B
TypeScript
Raw Normal View History

2023-09-17 23:32:14 +08:00
function use<T>(promise: PromiseLike<T>): T {
const internal: PromiseLike<T> & {
status?: 'pending' | 'fulfilled' | 'rejected';
value?: T;
reason?: any;
} = promise;
if (internal.status === 'fulfilled') {
2023-09-17 23:32:14 +08:00
return internal.value as T;
}
if (internal.status === 'rejected') {
throw internal.reason;
}
if (internal.status === 'pending') {
throw internal;
}
internal.status = 'pending';
internal.then(
(result) => {
internal.status = 'fulfilled';
internal.value = result;
},
(reason) => {
internal.status = 'rejected';
internal.reason = reason;
},
);
throw internal;
}
2023-09-17 23:32:14 +08:00
export default use;