0

In my React Native app I make an API call and then try to apply the json() method to the result, like this:

await fetch(...)
  .catch(e => {...})
  .then(res => res.json().then(...)

Typescript throws a warning on json() saying Property 'json' does not exist on type 'void | Response'.

What I Want To Know:

  1. Is there a way to prevent this warning?
  2. If I swap the order of catch and then, the error goes away. But I want catch to catch only errors from fetch(), not from the code in the then block. Is there a way to achieve this?
gkeenley
  • 6,088
  • 8
  • 54
  • 129
  • Are you actually *handling* the error in that `catch` block? It might make sense to just re-throw a different error with a better error message. – Bergi Jan 30 '22 at 06:11

2 Answers2

0

Use optional chaining:

await fetch(...)
  .catch(e => {...})
  .then(res => res?.json?.().then(...)

The optional chaining operator (?.) enables you to read the value of a property located deep within a chain of connected objects without having to check that each reference in the chain is valid.

jabaa
  • 5,844
  • 3
  • 9
  • 30
  • Can you explain what happens in this code? – gkeenley Jan 30 '22 at 02:45
  • @gkeenley I added a description of the operator. – jabaa Jan 30 '22 at 02:48
  • So does `res?.json()` call the `json` method only if `res` is valid, and `res?.json?.().then(...)` call `then(...)` only if the output of `json()` is valid? – gkeenley Jan 30 '22 at 02:48
  • @gkeenley `res?.json` reads `json` only if `res` isn't nullish and `res?.json?.()` calls `json` only if `res?.json` isn't nullish. It doesn't check the returned value of `json`. – jabaa Jan 30 '22 at 02:50
0

If I swap the order of catch and then, the error goes away. But I want catch to catch only errors from fetch(), not from the code in the then block. Is there a way to achieve this?

Yes, use .then(…, …) instead of .then(…).catch(…):

await fetch(...).then(res =>
  res.json().then(…)
, e => {
  …
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375