Making Asynchronous HTTP Requests with in Node.js

Feb 3, 2024 ยท 2 min read

The method in Node.js provides a convenient way to make HTTP POST requests. However, by default it is a synchronous method that will block the event loop while the request is in progress. Here are some techniques for making asynchronous and non-blocking:

Use a Callback

The easiest way is to provide a callback function as the last argument to This callback will be invoked once the request finishes:, data, callback);

function callback(error, response, body) {
  if (!error && response.statusCode === 200) {
    // Request succeeded

This allows other code to run while the request is in progress, avoiding blocking the event loop.

Return a Promise

Another option is to wrap the call in a Promise:

function makeRequest() {
  return new Promise((resolve, reject) => {, data, (error, response, body) => {
      if (error) return reject(error);


This provides a clean interface using async/await.

Use Async Library

The async library has useful abstractions like async.parallel() to run multiple asynchronous operations concurrently:

  function(callback) {, callback);
  function(callback) {, callback);
], function(err, results) {
  // all requests completed

This allows coordinating multiple requests avoiding nested callbacks.

In summary, Node.js enables asynchronous I/O out of the box if you structure your code properly. Using callbacks, promises or helper libraries like async unlocks this powerful capability for non-blocking HTTP requests.

Browse by tags:

Browse by language:

Tired of getting blocked while scraping the web?

ProxiesAPI handles headless browsers and rotates proxies for you.
Get access to 1,000 free API credits, no credit card required!