Categories
JavaScript Answers

How to fix await is not working for Node request module?

Spread the love

To fix await is not working for Node request module, we switch to the node-fetch module.

To use it, we write

const url = "http://www.example.com";
try {
  const response = await fetch(url);
  const json = await response.json();
  return { message: json.message, status: json.type };
} catch (error) {
  console.log(error);
}

to call fetch with the url to make a get request to it.

It returns a promise so we can use await to get the response.

And then we use await again to get the response body returned by the promise returned by response.json.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.