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?
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?
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 tothen
are optional, andcatch(failureCallback)
is short forthen(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.