How to stop / end / abort / cancel a request made by npm request module

Hello guys, just a small update.

Recently I took over a project developed months ago by another team. It was a NodeJS codebase with a lot of dependencies, one of which was the npm request module.

As a feature required us to stop the request midway (if some condition has been met), we realised we knew nothing about stopping the request!

Well indeed, at the time of writing, if you take a look at request’s documentation (which is also its README), you probably won’t find anything about stopping a request midway. Either that, or I have a serious issue with my eyesight.

Of course, this is just a matter of documenting. A bit of source code reading easily reveals that the request object has these methods available: end(), abort(), and destroy().

Out of the 3 that I tried, only abort() did what I wanted. I called it somewhere during the handler functions for the end and data events, and it did stop the request operation.

So, if you’re facing a similar case, I hope this may shed a bit light into matters at hand. I’ll probably open a pull request to update their documentation, but if any of you could do it, please go ahead & pay it forward! 🙂


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s