如何在nodejs中取消一个不太直接的请求

我有一个节点进程运行,不时地做一些unirest.get和unirest.post。 我也有一个Webterminaltypes,从中我可以看到所有不重要的请求的总体进度。

问题是我需要能够取消一个特定的请求,但我不知道如何。

结构是这样的:

var requests = []; requests.push(unirest.post('someurl').end(somecallback)); requests.push(unirest.post('someurl').end(somecallback)); requests.push(unirest.post('someurl').end(somecallback)); 

我想要做的事情如:

 requests[1].cancel(); // but of course this method doesn't exist 

因为在这种情况下,我不能让callback触发,因为目标是取消它再次请求相同的URL,而不取消它的callback会触发两次。

任何人都知道如何取消/中断/销毁它?

unirest.post('someurl').end(some callback)结果unirest.post('someurl').end(some callback)是来自请求模块的 Request对象。

所以,你可以使用abort 方法 :

 requests[1].abort();