I have the following function which makes an ajax request o fetch data from an API.
function getSegements(url) {
return new Promise((resolve, reject) => {
request = new XMLHttpRequest();
request.open('GET', url);
request.setRequestHeader('Content-Type', 'application/json');
// request.onload = () => resolve(request.response);
// request.onerror = () => reject(request.status);
request.onreadystatechange = function() {
if (request.readyState === 4)
{
if (request.status === 200)
{
data = JSON.parse(request.response);
console.log(data.segements);
resolve(data);
}
else
{
reject({status: request.status});
}
}
};
request.send();
});
}
calling the function:
getSegements(url).then((data) => {
//console.log(data);
//data = JSON.parse(data);
theWheel = new Winwheel({
'outerRadius' : 212,
'textFontSize' : 16,
'textOrientation' : 'horizontal',
'textAlignment' : 'outer',
'numSegments' : data.no,
'segments' : data.segements,
'animation' : // Specify the animation to use.
{
'type' : 'spinToStop',
'duration' : 5, // Duration in seconds.
'spins' : 3, // Default number of complete spins.
'callbackFinished' : alertPrize
}
});
theWheel.animation.spins = 9;
wheelSpinning = false;
})
.catch((err)=>{
console.log(err);
alert('Request failed. Returned status of ' + err.status);
});
When there is a fault in WinWheel's parameter it runs the catch block. Why is running like that? Doesn't it depend on the function (in this case getSegements) if then() is going to run or catch()?