0

im doing some online test , which i need to call a function , inside that function send an http request to an api and return its response

function callapi()
{
    require('https').get(`https://example.com/api/getdata`, (res) =>
    {
      res.setEncoding('utf8');
      res.on('data', function (body)
      {
        var data = JSON.parse(body);
        return(data);
      });
    });

}

sine the call is async i need to wrap it inside a promise so the function would return after reciving response

function callapi()
{
  return new Promise(function (resolve, reject)
  {
    require('https').get(`https://example.com/api/getdata`, (res) =>
    {
      res.setEncoding('utf8');
      res.on('data', function (body)
      {
        var data = JSON.parse(body);
        resolve(data);
      });
    });
  })

}

here is the problem , since resolve response is not readable if i console.log it i get

console.log(callapi());
Promise { <pending> }

i need to call it like

callapi.then(function (result)
{
  console.log(result)
})

so i can have a readable data , but the test doesn't allow me to change its structure and requires my function to return the data without needing to use .then to see the result ... so my question is how can return the data from function without needing to using .then to see the output

** ps : i dont have to use promise , any other solution will work **

hretic
  • 999
  • 9
  • 36
  • 78
  • 1
    *"so my question is how can return the data from function without needing to using `.then` to see the output"* You can't. What kind of online test is this? – Felix Kling Mar 17 '20 at 12:19
  • @FelixKling is correct. The only alternative is `async`/`await` but that's just using `.then` under the hood. And you still get a promise as the return value. – VLAZ Mar 17 '20 at 12:20

2 Answers2

0

I would suggest you try to use async and await.

eg: function async callapi()

and return await new Promise(function (resolve, reject)

jack1012t
  • 86
  • 6
  • 2
    If any the syntax is `async function callapi()`. But an `async` function just returns a promise. Since the function already returns a promise, there is no need to add `async` or `await`. In other words, `function foo() { return new Promise(...); }` and `async function foo() { return await return Promise(...); }` are equivalent for all intents and purposes. – Felix Kling Mar 17 '20 at 12:28
0

If you don't want to use the promise, then you can make a sperate function(which performs the operation on returned data) and passed it to callapi function as a parameter.

function callapi(callback)
{
    require('https').get(`https://example.com/api/getdata`, (res) =>
    {
      res.setEncoding('utf8');
      res.on('data', function (body)
      {
        var data = JSON.parse(body);
        //return(data);
        callback(data); 
      });
    });

}
Rajesh Verma
  • 309
  • 2
  • 8
  • how can i get the return with this solution ? i need to call the function and get a result without any other steps ... i can add another function but i have to call `callapi` first – hretic Mar 17 '20 at 12:28
  • when you are calling the function just pass the callback as params to perform the operation on returned data. Inside the callapi function, once the request is completed and response is received then callback function will execute with received data. – Rajesh Verma Mar 17 '20 at 12:36
  • iu know that , but it wouldn't return the function result to the caller ... i need these lines give me the result `var result = callapi(); console.og(result);` – hretic Mar 17 '20 at 12:39