Search code examples
node.jsasync-awaitecma

node js undefined value return from http get async function


I am trying to get the http get response from async function.However within the function value is displaying but return values is undefined.

Even promise not undefined values

please find code below

'use strict';
const express = require('express');
    var request = require('request');
var https = require('https');

async function getCurrencies() {
    let    response;
    try {


        var getOptions = {
            url: 'http://localhost:3000/api/currency',
            method: 'GET',
            headers: {
                'Content-Type': 'application/json'
            },
            json:true
        };

        await request(getOptions, function (err, res, body) {

            if (res && (res.statusCode === 200 || res.statusCode === 201)) {
        console.log(' response ', res.body.rates.INR);

            return res.body;
            } else {
                console.log('error body ', body);

            }
        });

    } catch (error) {
        console.log(" error pulling ", error);
        process.exit();
    }

}

var tt =  getCurrencies().then(function(value) {
    console.log(' tt values ',value);

}
);

below is the logs

 tt values  undefined
 response 64.945

Solution

  • I'd rewrite to do something like this:

    function getCurrencies() {
    
        return new Promise((resolve, reject) => {
            try {
    
                var getOptions = {
                    url: 'http://localhost:3000/api/currency',
                    method: 'GET',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    json:true
                };
    
                request(getOptions, function (err, res, body) {
    
                    if (res && (res.statusCode === 200 || res.statusCode === 201)) {
                        console.log(' response ', res.body.rates.INR);
                        resolve(res.body);
                    } else {
                        console.log('error body ', body);
                        reject(new Error('Error body: ' + JSON.stringify(body)));
                    }
                });
    
            } catch (error) {
                console.log(" error pulling ", error);
                process.exit();
            }
        });
    }
    
    getCurrencies().then(function(value) {
        console.log(' tt values ',value);
    });
    

    You can also do something a bit more compact:

    const rp = require('request-promise');
    function getCurrencies() {
    
        var getOptions = {
            url: 'http://localhost:3000/api/currency',
            method: 'GET',
            headers: {
                'Content-Type': 'application/json'
            },
            json:true,
            resolveWithFullResponse: true 
        };
    
        return rp(getOptions).then((response) => {
            return response.body;
        });
    }
    
    getCurrencies().then(function(value) {
        console.log(' tt values ',value);
    }).catch ((err) => {
        console.error('An error happened: ' + err);
    });