gaearon/overreacted.io

Algebraic effects post. Note about the code example.

ggarek opened this issue · 0 comments

The article is very inspiring and well written. I enjoyed reading a lot, thank you! ❤️

This is just a small note regarding the code example, used in the article.

This code:

function getName(user) {
  let name = user.name;
  if (name === null) {
  	throw new Error('A girl has no name');
  }
  return name;
}

function makeFriends(user1, user2) {
  user1.friendNames.add(getName(user2));
  user2.friendNames.add(getName(user1));
}

const arya = { name: null };
const gendry = { name: 'Gendry' };
try {
  makeFriends(arya, gendry);
} catch (err) {
  console.log("Oops, that didn't work out: ", err);
}

, will throw earlier than throw new Error('A girl has no name');.
It will throw in user1.friendNames.add(getName(user2));.

I was just a bit confused reading the code and text, quote:

We throw inside getName, but it “bubbles” up right through <...>