I have a question about node.js request

Asked 2 years ago, Updated 2 years ago, 53 views

I made a function because I wanted to return the data received by request(get) in a variable If you take a function,

Promise { <pending>} 

It pops up like this. I don't know what to do, so I'm asking you a question!

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"));

If you take a response in then, the value comes out well <

node.js

2022-09-20 08:43

1 Answers

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

function get_data (url){
  return new Promise((resolve, reject)=>{
    rq({
      method: "GET",
      url: url
    })
    .then((res)=>{
      resolve(res);
    })
    .catch((err)=>{
      reject(err)
    });
  });
}

getData("http://somewhere/get_gameData/OXquiz")
.then((res)=>{
  console.log(res);
})
.catch((err)=>{
  console.error(err);
});


2022-09-20 08:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.