Question:
Is there an “easy” way to undo ( $q - / $http -) promises in AngularJS or determine the order in which promises are allowed?
Example
I have a long calculation and I am requesting the result via $http . Some actions or events require me to restart the calculation (and thus send a new $http request) before the initial promise is resolved. Thus, I think I can not use a simple implementation, for example
$http.post().then(function(){
because I cannot guarantee that the responses are returned in the order in which I sent the requests. In the end, I want to show the result of the last calculation when all promises were correctly resolved.
However, I would like to avoid waiting for the first response until I submit a new request, for example:
const timeExpensiveCalculation = function(){ return $http.post().then(function(response){ if (isNewCalculationChained) {return timeExpensiveCalculation();} else {return response.data;} }) }
Thoughts:
When using $http I can access the configuration object in the response to use some timestamps or other identifiers to manually order incoming responses. However, I was hoping that I could just say angular somehow cancel the deprecated promise and thus not run the .then () function when it is resolved.
This does not work without a manual implementation for $q - promises instead of $http .
Maybe just abandoning the promise right away is the way to go? But in both cases, this can go on forever until, finally, the promise is resolved before the next request is formed (which leads to an empty view in the meantime).
Is there some kind of angular API function that I will miss, or are there robust design patterns or “tricks” with a promise chain or $ q.all to handle multiple promises that return “the same” data?