Skip to content

Instantly share code, notes, and snippets.

@lightsofapollo
Created August 11, 2016 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lightsofapollo/b8352ea0f1d2c1b82622c90b3d8094e8 to your computer and use it in GitHub Desktop.
Save lightsofapollo/b8352ea0f1d2c1b82622c90b3d8094e8 to your computer and use it in GitHub Desktop.
type Result<E, V> = [null, V] | [E, null];
const emailList = ['james@silklabs.com'];
function searchForEmail(email: string): Result<Error, bool> {
if (email.indexOf('@') === -1) {
return [new Error('this is not an email'), null];
}
// here we would do an actual search for the email ...
return [null, emailList.indexOf(email) !== -1];
}
const [err, hasEmail] = searchForEmail('foo@bar.com');
if (err) {
console.log(`invalid email: ${err.stack}`);
} else {
console.log('found email address: ', hasEmail);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment