15
Apr

Understanding callback functions in javascript

Callback functions in javascript are also called “Higher order functions”.

In our code, if we want to execute a function right after the return of some other function, then callbacks can be used.

eg. myFunctionWhichCallback(param1, param2, callBackFunctionName)
{
console.log(`Two numbers entered are ${param1} and ${param2}`)
// now execute your callBackFunction
callBackFunctionName();
}

As in javaScript functions are of type “Objects” they can be passed as Parameter to any javascript function.

Following code snippet demonstrates how to use callback concept in Jest
————————————————————————

describe(“Functions to test return value of function in JavaScript”, function(){

function add(param1, param2 , callback, callback1){
console.log(`The numbers are ${param1} and ${param2}`);
callback();
callback1();
}

function disp(){
console.log(‘disp function called’);
}

function jack(){
console.log(‘Jack function called’);
}

test(‘Expect true from return value of fetchData()’, ()=> {
var x = add(5,6,jack, disp);
});

})