Why await is not working for node request module?

You should only await on something that returns a Promise. I would definitely recommend reading up on Promises before you start working with async and await. You can probably get this example to work by creating your own wrapper function around request to make it return a promise, like so:

function doRequest(url) {
  return new Promise(function (resolve, reject) {
    request(url, function (error, res, body) {
      if (!error && res.statusCode === 200) {
      } else {

// Usage:
async function main() {
  try {
    let response = await doRequest(url);
    console.log(response); // `response` will be whatever you passed to `resolve()` at the top
  } catch (error) {
    console.error(error); // `error` will be whatever you passed to `reject()` at the top


Edit: Alternatively, you can look into using a promise-based request library instead of the regular request module.

Leave a Comment