Sonar code quality rules in Adobe cloud manager
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)}
geoCode('New Delhi', (callbackData) => {console.log(callbackData)});
{ location: 'New Delhi', latitude: 0, longitude: 0 }
Immediately Invoked Function Expression (IIFE) in JavaScript
There is a situation where we want to make a function and execute that function immediately at the same place where we have written. These function can be written in JavaScript without any function name. The approach which we follow to do so is called Immediately Invoked Function Expression (IIFE). Let's see an example of this.
(function() {
//write something in IIFE
}
Hence, if a develop come to any point where there is no need to function name then they could use Immediately Invoked Function Expression (IIFE).
How to write JavaScript function without function name? and Write an immediate invoke function in JavaScript?
Answer of all these questions is Immediately Invoked Function Expression.