Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why await is not working for node request module?

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?

like image 252
user43286 Avatar asked Jul 18 '16 03:07

user43286


People also ask

How do you request await?

You need to use the request-promise module, not the request module or http. request() . await works on functions that return a promise, not on functions that return the request object and expect you to use callbacks or event listeners to know when things are done.

How does await work in Nodejs?

The await expression causes async function execution to pause until a Promise is settled (that is, fulfilled or rejected), and to resume execution of the async function after fulfillment. When resumed, the value of the await expression is that of the fulfilled Promise .

Does await block Nodejs?

async/await does not block the whole interpreter. node. js still runs all Javascript as single threaded and even though some code is waiting on an async/await , other events can still run their event handlers (so node. js is not blocked).

Can I use async await in node?

Async functions are available natively in Node and are denoted by the async keyword in their declaration. They always return a promise, even if you don't explicitly write them to do so. Also, the await keyword is only available inside async functions at the moment – it cannot be used in the global scope.


2 Answers

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() {   let res = await doRequest(url);   console.log(res); }  main(); 

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

like image 197
Saad Avatar answered Sep 21 '22 12:09

Saad


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)         })     }) 
like image 45
dimpiax Avatar answered Sep 20 '22 12:09

dimpiax