0

CASE 1:

fetch('/foo')
   .then((res) => console.log(res), err => console.log(err))

CASE 2:

fetch('/foo')
   .then(res => console.log(res))
   .catch(err => console.log(err))

What is the difference between both these cases?

Tibebes. M
  • 6,940
  • 5
  • 15
  • 36

1 Answers1

1

the first one is without chaining and the second one

fetch('/foo')
       .then(res => console.log(res))
      .catch(err => console.log(err))

is using promise chaining and .catch really means .then(null, handler)

check this MDN page (under chaining section)

...
The arguments to then are optional, and catch(failureCallback) is short for then(null, failureCallback). You might see this expressed with arrow functions instead:

In other words, the second one is simply a short hand for:

fetch('/foo')
       .then(res => console.log(res))
       .then(null, err => console.log(err))

So the difference is simply the use of chaining.

Tibebes. M
  • 6,940
  • 5
  • 15
  • 36