Menu

Showing posts with label Callback NodeJS. Show all posts
Showing posts with label Callback NodeJS. Show all posts

Callback function JavaScript and NodeJS

Lets understand the callback fucntion in NodeJS using the below geocode example.

Here we have created geoCode function with two paramters, first parameter will take input of the location name and second parameter the function that needs to call. Similary, we can add or pass any number of paramters in fucntion as per need.

const geoCode = (location, callback) => {
    const data = {
        location: location,
        latitude: 0,
        longitude: 0
    }
    callback(data)
}

 

When we call the geoCode function, we are here passing a city name and second paramter a fucntion itself that print the return data from callback on console.
 
geoCode('New Delhi', (callbackData) => {
    console.log(callbackData)
});

Return

{ location: 'New Delhi', latitude: 0, longitude: 0 }