I've got a simple JQuery event handler that is triggered when a form is submitted.
$('.selector').on('submit', function(e) {
e.preventDefault();
submitForm(status).then(updateData());
});
I'm trying to add the capability to cancel the second call if the first one meets a certain condition but I can't get it working. I found this question but for some reason the second function still executes when reject()
is returned. Rejecting A jQuery Promise In A $.ajax Success Method.
function submitForm(status) {
return $.ajax({
method: "POST",
url: "/url/to/use",
data: formData,
dataType: "json",
contentType: false,
processData: false
}).then(function(data) {
if (data.error === true) {
return new $.Deferred().reject().promise();
} else {
// do other stuff
}
});
}
Do I need to create a deferred object and return it or is that done by just calling then()
? I've read quite a bit about deferred and promises but still can't understand how they work.