68

I'm new to nodejs. I’m not seeing the response in ex 1, but i see in ex 2. Why? Await works for me in other places, using babel.

Ex 1

 let res = await request(url)
 console.log(res);
 console.log(res.body);

Ex 2

request(url, function (error, res, body) {
 if (!error && response.statusCode == 200) {
 console.log(body) 
 }
});

Await works in other places, I’m using babel and required modules for es6 and es7 features. For example, await works in squelize call, i validated. But it doesn’t work for request call. Why?

2
  • FYI, async/await is not part of ES7. It's still only a proposal. Jul 18, 2016 at 8:06
  • See my answer here. Additionally, this is helpful too.
    – David Pine
    Jul 18, 2016 at 12:06

5 Answers 5

201

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) {
        resolve(body);
      } else {
        reject(error);
      }
    });
  });
}

// 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
  }
}

main();

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

5
  • 8
    this should be the answer
    – felipekm
    Oct 31, 2017 at 13:38
  • How about using promisify from util? Jun 23, 2019 at 11:38
  • Really Helpful.
    – iksheth
    Oct 18, 2019 at 8:16
  • request-promise has been deprecated since then
    – J. Chan
    May 22, 2020 at 1:36
  • Thanks j-chan, I updated the answer based on your comment.
    – Saad
    May 26, 2020 at 20:19
25

ES6

Usage: Where request is require('./await-request')

const init = async () => {
    try {
        const result = await request({
            uri: 'statdirectory/exchange?json',
            baseUrl: 'https://bank.gov.ua/NBUStatService/v1/',
            json: true
        })
        console.log(result)
    }
    catch (err) {
        console.error(err)
    }
}

Code:

// await-request.js
const request = require('request')

module.exports = async (value) => 
    new Promise((resolve, reject) => {
        request(value, (error, response, data) => {
            if(error) reject(error)
            else resolve(data)
        })
    })
3
  • 4
    you mean async-request not await-request? right? npmjs.com/package/async-request
    – HMagdy
    Mar 11, 2019 at 19:11
  • 2
    @HMagdy no, await-request it's a filename of local file.
    – dimpiax
    Mar 11, 2019 at 20:22
  • 1
    aha, yep got it
    – HMagdy
    Mar 12, 2019 at 11:21
24

As @saadq says you can only 'await' functions returning Promise.

I like using Node.js's util package to promisify a function with callback. So util + request can be used like that:

const util = require('util')
const request = require("request");

const requestPromise = util.promisify(request);
const response = await requestPromise(url);
console.log('response', response.body);

Demo: https://runkit.com/mhmtztmr/node-js-request-with-async-await

1
  • This is just what I needed for a lambda -- simple and doesn't require anything other than Node
    – jcollum
    Jun 21, 2019 at 22:06
2

Try with the following NPM package

node-fetch

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

Hope it works.

0

need to add await to the response.json() because response.json() is promise

( async function(){
      const{default:fetch} = await import('node-fetch');
      try {
        response = await fetch('https://datausa.io/api/data?drilldowns=Nation&measures=Population')  ;
        console.log(await response.json())
      } catch (err) {
        console.log('Http error', err);
      }
    })()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.