I am trying to create a Javascript promise for a ajax call. If it returns the right response, I am resolving it and had attached the corresponsing then block to the Promise. If it rejects, I have a catch block for that. Unfortunately, for me, eventhough i reject, the then block in the promise gets executed. Please help me address this. Below is the code block I am using in my app.
function check (){
return new Promise ((resolve, reject) => {
$.ajax({
type:"get",
url : "API",
data:{"jsonData":"validJsonData"},
async:true,
success:function(responseMap){
if(responseMap.data && JSON.parse(responseMap.data)) {
resolve(responseMap)
}else{
reject(responseMap.message)
}
},error : function(errorObj){
reject(errorObj)
}
}).then(result => {
let obj= JSON.parse(result.data);
// process obj
loader.hide();
}).catch(error => {
console.log(error)
// error handle logic
loader.hide();
});
});
}