Vous pouvez vérifier le content-typede la réponse, comme indiqué dans cet exemple MDN :
fetch(myRequest).then(response => {
  const contentType = response.headers.get("content-type");
  if (contentType && contentType.indexOf("application/json") !== -1) {
    return response.json().then(data => {
      // process your JSON data further
    });
  } else {
    return response.text().then(text => {
      // this is text, do something with it
    });
  }
});
Si vous devez être absolument sûr que le contenu est un JSON valide (et ne pas faire confiance aux en-têtes), vous pouvez toujours accepter la réponse en tant que textet l'analyser vous-même:
fetch(myRequest)
  .then(response => response.text())
  .then(text => {
    try {
        const data = JSON.parse(text);
        // Do your JSON handling here
    } catch(err) {
       // It is text, do you text handling here
    }
  });
Asynchroniser / attendre
Si vous utilisez async/await, vous pouvez l'écrire de manière plus linéaire:
async function myFetch(myRequest) {
  try {
    const reponse = await fetch(myRequest); // Fetch the resource
    const text = await response.text(); // Parse it as text
    const data = JSON.parse(text); // Try to parse it as json
    // Do your JSON handling here
  } catch(err) {
    // This probably means your response is text, do you text handling here
  }
}