Callback Function in Node
Using Arrow function:
Using Callback in the Http Request to the API
const SayHello = ( Mycallback ) =>{
setTimeout(() => {
const data ="hello"
Mycallback(data)
}, 2000);
}
SayHello( function(data){
console.log(data)
} )
Using Arrow function:
const geocode = (Mycallback) => {
setTimeout(() => {
const data = "hello";
Mycallback(data);
}, 2000);
};
geocode((data) => {
console.log(data);
});
Using Callback in the Http Request to the API
const request = require("postman-request");
const getusers = (Mycallback) => {
request("https://reqres.in/api/users?page=2", function ( error,response,body ) {
if (error) {
return console.log(error);
}
if(body){
Mycallback(body)
}
});
};
getusers((data) => {
console.log(data);
});
0 Comments