1

I'm trying to use request to make a request to this api https://api.ipify.org/?format=json and receive back some json like this {"ip":"XX.XX.XXX.XX"} from there I want to parse it and let my function's callback console.log the ip.

I'm just learning about callbacks and async so please give me any advice you can :)

const url = 'https://api.ipify.org/?format=json';

const getMyIP = function (callback) {
  request(url, (error, body, _response) => {
    body = JSON.parse(body);
    const ip = body["ip"];
    return ip;
  });
};


getMyIP((error, ip) => {
  if (!error) {
    console.log(ip);
  }
});
1
  • 1
    instead of retuning ip, just call your callback. Commented Aug 2, 2020 at 4:17

1 Answer 1

1

fetch("https://api.ipify.org/?format=json")
.then(res => res.json())
.then(val => {console.log(val.ip);})
.catch(e => console.log(`Error - ${e}`))

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.