How to abort fetch?

Tell me, how not to start in second then if the first has got a certain status?
Code
fetch('https://example/api', options)
 .then(function (response) {
console.log(response)
 if (response.status === 200) {
 console.log('Successfully connected to API')
 return response.json();
 } else if (response.status === 300) {
 console.log('queued');
 } else {
 // here to interrupt and fetch are not allowed in the following then
 console.log('Error', response);
}

})
 .then(function (data) {
 // to do something
})
 .catch((error) => {
 console.log('connection Error');
console.log(error)
 });
March 19th 20 at 08:51
3 answers
March 19th 20 at 08:53
Solution
`throw new SomeError()`
March 19th 20 at 08:55
Solution
} else {
 // here to interrupt and fetch are not allowed in the following then
 throw new Error(response)
}
March 19th 20 at 08:57
Solution
todoSmth function() {}

try {
 const response = await fetch("https://example/api");

 if (response.status === 200) {
 const json = await response.json();
todoSmth();
 } else if (response.status === 300) {
 console.log('queued');
todoSmth();
 } else {
 console.log('Error', response);
}

}
catch (err) {
console.error(error);
}


Or returns an object where the data will be and status, and the second then decide what to do.

https://javascript.info/promise-chaining

Find more questions by tags JavaScript