-1

I want to return the result obtained from the request.

The code does not produce the desired result.

Please help me

CODE

const getData = async (gn) => {
  let gameData = undefined;

  gameData = await rq({
    method: "GET",
    url: base_url + "/get_gameData/OXquiz",
  }).then(function (response) {
    return response;
  });

  return gameData;
};

console.log(getData("OXquiz"));

RESULT

Promise { <pending> }

The result I want

{"status":0,"message":[{"ga_win_point":"100","ga_lose_point":"50","ga_time":"10","ga_max_number":"8"}],"url":""}
  • Does this answer your question? [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). TL;DR you want `getData("OXquiz").then(console.log)` – Phil Jul 18 '22 at 05:13
  • Your entire function can be simplified to `const getData => (gn) => rq({ method: "GET", url: \`${base_url}/get_gameData/${gn}\` })` – Phil Jul 18 '22 at 05:14

2 Answers2

0

try

getData().then(item => console.log(item))

or

const data = await getData()
Jawak
  • 1
  • 1
-1

Why are you using await and .then at a time. If you want to work asynchronously use the following snippet.

const getData = async (gn) => {
  let gameData = undefined;

  gameData = await rq({
    method: "GET",
    url: base_url + "/get_gameData/OXquiz",
  });

  return gameData;
};

console.log(getData("OXquiz"));
Aravind
  • 600
  • 5
  • 12