Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class Throws<T> {
constructor(public error?: T) {
}
then<S>(fn: () => Throws<S>): Throws<T | S> {
if (this.error) {
// already errored, don't attempt to run the next action
return this;
}
const result = fn();
return result;
}
static resolve<T>(error?: T) {
return new Throws<T>(error);
}
};
let { error } = Throws.resolve<never>().then(() => {
if (Math.random() > 0.5) {
return Throws.resolve(new TypeError('lost'));
}
}).then(() => {
if (Math.random() > 0.5) {
return Throws.resolve(new ReferenceError('Got a reference error'));
}
return Throws.resolve(new RangeError('range'));
}).then(() => {
return Throws.resolve(new Error('Shouldn\'t get here!'))
});
typeof error; // TypeError | Error | ReferenceError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment