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);
 }
});
hoangdv
16.3k4 gold badges31 silver badges54 bronze badges
asked Aug 2, 2020 at 4:14
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}`))

answered Aug 2, 2020 at 4:18
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.