0
const https = require('https');
const request = require('request');
process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', function(inputStdin) {
    inputString += inputStdin;
});

process.stdin.on('end', function() {
    inputString = inputString.split('\n');

    main();
});

function readLine() {
    return inputString[currentLine++];
}

async function getCountryName(code) {
    for (var i = 1; i<26; i++)
    {
    const url= 'https://jsonmock.hackerrank.com/api/countries?page='+i
     await request({url,json:true}, (error,{body})=>{
         for(var i=0; i<10 ; i++)
         {
             if (body.data[i].alpha2Code == code)
             {
                console.log(body.data[i].name)
                return body.data[i].name
                break
             }
         }
     })}
    //  return name
}

async function main() {
  const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

  const code = readLine().trim();

  const name = await getCountryName(code);

  ws.write(`${name}\n`);

}

when i run this program, it showing undefined as a result instead of place name. But when I log it showing correct value.

Below is the intelligence when I keep my cursor on getCounrtyName function.

function getCountryName(code: any): Promise<void>

I think it should be sent as a response to the promise and I don't know that.

Suryaprakash
  • 80
  • 1
  • 11
  • When you do `return` in the callback you're passing `request`, all that does is return a value **from the callback**. It has no effect on `request`. There's also no reason to use `await` with `request` since it doesn't return a promise. See [the linked question](https://stackoverflow.com/questions/22519784/how-do-i-convert-an-existing-callback-api-to-promises) for how you might wrap it in something providing a promise -- or better yet, use a non-deprecated package like [`node-fetch`](https://www.npmjs.com/package/node-fetch) that supports promises directly. Happy coding! – T.J. Crowder Jul 31 '20 at 11:35
  • Related reading: [*How do I return the response from an asynchronous call?*](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – T.J. Crowder Jul 31 '20 at 11:35
  • Also, if you want `getCountryCode` to return the fulfillment value of the promise (once you have one), you need to use `return` in that function. – T.J. Crowder Jul 31 '20 at 11:36
  • Also note that you have conflicting uses of `i`. Because you've used `var`, it's the same `i` variable **throughout** the function, you can't use it to control nested loops. You'd need separate variables. (I recommend using different names even if you switch to `let`, where technically you *could* use the same name, but...) FWIW, here's a copy of `getCountryCode` with all my various comments applied: https://pastebin.com/x9rihSih Happy coding! – T.J. Crowder Jul 31 '20 at 11:43
  • (FWIW, I'm not convinced that your `readLine` function is reliably going to have lines to read -- you have a race condition between the stream and your loop -- but I haven't debugged it.) – T.J. Crowder Jul 31 '20 at 11:44
  • 1
    Thank you . Your comments are very helpful – Suryaprakash Aug 01 '20 at 15:11

0 Answers0