I have this ajax call and I need to turn it into a promise.
function myRest(url, method, callback){
return $.ajax({
url : url,
type : method,
dataType: "json",
contentType: "application/json",
success: function(results){
//things to do in case of success
},
error: function (){
//things to do in case of error
}
});
}
How could I also use the .then() method in case of success? Thanks
I have this ajax call and I need to turn it into a promise.
$.ajax()
already returns a promise. You can just use .then()
on it already, return the promise and get rid of the callback. You don't need to "turn anything into a promise". It already is a promise. You can use that existing promise like this:
function myRest(url, method) {
return $.ajax({
url : url,
type : method,
dataType: "json"
});
}
myRest(...).then(function(results) {
// success
}, function(jqXHR, textStatus, errorThrown) {
// error
});