I have a single page app that gets a list of objects from an node js server. However if the user selects the first item in the list before the rest of the related objects are loaded it needs to cancel the request. I can cancel the request on the client side (angular) but don't see how to cancel a request on an already started call in node !
The code below is the method i use to call the node api with the ability to cancel that request on the client side.
getDiscovery : function(ids) {
var Ids = [],
apiUrl = '/api/Something/ids',
self = this,
deferredAbort = $q.defer();
// Initiate the AJAX request.
var request = $http({
method: "get",
url: apiUrl,
params: {
ids: JSON.stringify(ids)
},
timeout: deferredAbort.promise
});
var promise = request.then(
function(response) {
return(response.data);
},
function(response) {
return($q.reject( "Something went wrong" ));
}
);
promise.abort = function() {
deferredAbort.resolve();
};
promise.finally(
function() {
console.info( "Cleaning up object references." );
promise.abort = angular.noop;
deferredAbort = request = promise = null;
}
);
return(promise);
}
This is the method on the node api which needs to be cancelled
exports.getItemsDiscovery = function(req, res){
var Items = JSON.parse(req.query.ids);
async.each(Items,
function(Item, callback){
method.getSomething(Item.Id, function(data){
console.log('Got data for', item.Id);
callback();
});
},
function(err) {
return res.send(Items);
});
};
The route is
app.get('/api/ids', something.getItemsDiscovery);
Any help would be much appreciated
Rob