I am creating form validation and recognizing promises. I decided to implement asynchronous validation functions using the promise template:
var validateAjax = function(value) { return new Promise(function(resolve, reject) { $.ajax('data.json', {data: {value: value}}).success(function(data, status, xhr) { if (data.valid) { resolve(xhr) } else { reject(data.message) } }).error(function(xhr, textStatus) { reject(textStatus) }) }) }
It seems that this works fine, the input is checked as user types (the code is simplified so as not to be too long, for example, a timeout after a key is missing, etc.).
Now I am wondering how to kill an ajax request if the verification from the previous keyup event is still ongoing. Is it possible to somehow determine in what state the promise is and, perhaps, to reject the promise from the outside?
source share