Does the throw new Error get executed always in this code?

Consider the following code:

fetch(url, {
    method: 'POST',
    headers: {
      'Content-type': 'application/json',
      'apikey': apiKey
    body: data
  }).then(response => {
    if (response.ok) {
      return response.json();
    throw new Error('Request failed!');
  }, networkError => {

Does the throw new Error part always get executed because no else statement is used? Or if it doesn’t get executed what will be the reason?


The else is not needed since there is a return in the truthy branch of the if. When that line is followed, it means the response was okay.


Ok now I understood, Thanks :heart:


You’re welcome.

This is a common code pattern. The gotcha is that else is needed when return is not used.

I’m a big fan of return in conditional statements.

Inline code

if (x > 0) (
  console.log(`${x} is positive.`)

We could even dress up the inline code with a ternary expression…

console.log(`${x} is ${x > 0 ? `positive` : x < 0 ? `negative` : `zero`}.`)

Only problem is, inline code usually only executes once. What we end up with is spaghetti code if we persist in coding inline. All languages give us functional alternatives.

const isPositive = x => x > 0;
const isNegative = x => x < 0;
console.log(`${x} is ${isPositive(x) ? `positive` : isNegative(x) ? `negative` : `zero`}.`)

And still further down that rabbit hole…

const getPolarity = x => {
  const f = x => x > 0;
  const g = x => x < 0;
  return `${x} is ${f(x) ? `positive` : g(x) ? `negative` : `zero`}.`
x = 5

It may all be new to you now, but check back with me in a month and show me your newest find.

I am knowing this for the first time that return stops the execution of the function…
Anyways, I gained new knowledge.

1 Like

Sometimes I don’t understand some words that you write…
I guess because I am not an American…

1 Like

When a function is executed, it not only ceases, its data ceases to exist. All of it is garbage collected and all that remains is the source code. In JavaScipt that is raw text. The next time this function is run, it will have to be parsed, interpreted, bytecoded, and compiled. That is why JavaScript is so slow compared to Java and C.

But we don’t care about that. JS is very fast on the scale we need it, user interactions. It’s a behavior model applied to a UX. Should we need faster computing we can always lean on the server and its lower level languages. When anyone complains that JavaScript is too slow, simply tell them to use a different language. It’s fast enough for the stuff we really need it to do.

Hmmm, I started with JavaScript because everyone starts with Python and I wanted to do something different. Later I started hearing about the downsides of JavaScript, but still I love JavaScript because I only make small applications and it doesn’t have noticeable slowness.

1 Like

The end goal with JS is UX. We don’t use it for its processing prowess, but for its access to the DOM and the server, via AJAX. If you are not aiming at building user interfaces, then you can pass on this language. Python is the way to go for a beginner, and if you are a real serious beginner, Ruby. They both have their own flavor that relates back to HTML (Django and Rails, respectively). All that does is add computing power to a UX. If our UX doesn’t need that, then why bother?

As a dabbler in all those languages, as much as I love Ruby, I’ve adopted Python for the simplicity of it. No knocks on Ruby’s simplicity, but it is also more abstracted than Python, which is quicksand to a learner.

Python has less quicksand. It is a powerful language on so many levels, especially when the libraries come into play. On the learner level it has very simple syntax with only one rather major requirement: block definition. We must define our blocks by using indentation. Until this language came around, we indented for the purposes of pretty print. Python decided to use that practice as a way to define actual blocks and discarded the braces that many other languages use.

That hurdle aside, you’ll love the language for simple computing, on up.

Me, I love them all, and still have a very soft spot for JS.

Until one gets to know you, the language I use speaks to a more informed learner, to be fair. I’ve never really learned how to moderate my own language. It comes from a place of believing you will understand it. For that measure of hubris, please accept my apology. Some of the language will become second nature for you, in time, but I do rather rush ahead, which is my greatest weakness.

1 Like

Going to make this a public topic, presently. There’s nothing personal disclosed and it really should be a public discussion, not a private one.

I don’t mind making it public…

1 Like

It is nothing to bid apology for. It is my problem that I don’t understand them :smiley:

1 Like

We’re here to get you over the language hurdle. Never be afraid to challenge or question a response. Once we understand, it’s a whole new universe.

1 Like