type Status = 'pending' | 'fulfilled' | 'rejected';

interface ExtendedPromise<T> extends Promise<T> {
	status?: Status;
	value?: T;
	reason?: any;
}

function use<T>(promise: ExtendedPromise<T>): T {
	if (promise.status === 'fulfilled') {
		return promise.value as T;
	} else if (promise.status === 'rejected') {
		throw promise.reason;
	} else if (promise.status === 'pending') {
		throw promise;
	} else {
		promise.status = 'pending';
		promise.then(
			result => {
				promise.status = 'fulfilled';
				promise.value = result;
			},
			reason => {
				promise.status = 'rejected';
				promise.reason = reason;
			}
		);
		throw promise;
	}
}

export default use
