2023-09-17 23:32:14 +08:00
|
|
|
function use<T>(promise: PromiseLike<T>): T {
|
2023-08-03 14:45:51 +08:00
|
|
|
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;
|
2023-07-24 16:59:35 +08:00
|
|
|
}
|
2023-08-03 14:45:51 +08:00
|
|
|
if (internal.status === 'rejected') {
|
|
|
|
throw internal.reason;
|
|
|
|
} else if (internal.status === 'pending') {
|
|
|
|
throw internal;
|
2023-07-24 16:59:35 +08:00
|
|
|
} else {
|
2023-08-03 14:45:51 +08:00
|
|
|
internal.status = 'pending';
|
|
|
|
internal.then(
|
2023-07-24 16:59:35 +08:00
|
|
|
(result) => {
|
2023-08-03 14:45:51 +08:00
|
|
|
internal.status = 'fulfilled';
|
|
|
|
internal.value = result;
|
2023-07-24 16:59:35 +08:00
|
|
|
},
|
|
|
|
(reason) => {
|
2023-08-03 14:45:51 +08:00
|
|
|
internal.status = 'rejected';
|
|
|
|
internal.reason = reason;
|
2023-07-24 16:59:35 +08:00
|
|
|
},
|
|
|
|
);
|
2023-08-03 14:45:51 +08:00
|
|
|
throw internal;
|
2023-07-24 16:59:35 +08:00
|
|
|
}
|
|
|
|
}
|
2023-09-17 23:32:14 +08:00
|
|
|
|
|
|
|
export default use;
|