COURSES
MBAData Science & AnalyticsDoctorate Software & Tech AI | ML MarketingManagement
Professional Certificate Programme in HR Management and AnalyticsPost Graduate Certificate in Product ManagementExecutive Post Graduate Program in Healthcare ManagementExecutive PG Programme in Human Resource ManagementMBA in International Finance (integrated with ACCA, UK)Global Master Certificate in Integrated Supply Chain ManagementAdvanced General Management ProgramManagement EssentialsLeadership and Management in New Age BusinessProduct Management Online Certificate ProgramStrategic Human Resources Leadership Cornell Certificate ProgramHuman Resources Management Certificate Program for Indian ExecutivesGlobal Professional Certificate in Effective Leadership and ManagementCSM® Certification TrainingCSPO® Certification TrainingLeading SAFe® 5.1 Training (SAFe® Agilist Certification)SAFe® 5.1 POPM CertificationSAFe® 5.1 Scrum Master Certification (SSM)Implementing SAFe® 5.1 with SPC CertificationSAFe® 5 Release Train Engineer (RTE) CertificationPMP® Certification TrainingPRINCE2® Foundation and Practitioner Certification
Law
Job Linked
Bootcamps
Study Abroad
MS in Data AnalyticsMS in Project ManagementMS in Information TechnologyMasters Degree in Data Analytics and VisualizationMasters Degree in Artificial IntelligenceMBS in Entrepreneurship and MarketingMSc in Data AnalyticsMS in Data AnalyticsMS in Computer ScienceMaster of Science in Business AnalyticsMaster of Business Administration MS in Data ScienceMS in Information TechnologyMaster of Business AdministrationMS in Applied Data ScienceMaster of Business Administration | STEMMS in Data AnalyticsM.Sc. Data Science (60 ECTS)Master of Business AdministrationMS in Information Technology and Administrative Management MS in Computer Science Master of Business Administration MBA General Management-90 ECTSMSc International Business ManagementMS Data Science Master of Business Administration MSc Business Intelligence and Data ScienceMS Data Analytics MS in Management Information SystemsMSc International Business and ManagementMS Engineering ManagementMS in Machine Learning EngineeringMS in Engineering ManagementMSc Data EngineeringMSc Artificial Intelligence EngineeringMPS in InformaticsMPS in Applied Machine IntelligenceMS in Project ManagementMPS in AnalyticsMS in Project ManagementMS in Organizational LeadershipMPS in Analytics - NEU CanadaMBA with specializationMPS in Informatics - NEU Canada Master in Business AdministrationMS in Digital Marketing and MediaMSc Sustainable Tourism and Event ManagementMSc in Circular Economy and Sustainable InnovationMSc in Impact Finance and Fintech ManagementMS Computer ScienceMS in Applied StatisticsMaster in Computer Information SystemsMBA in Technology, Innovation and EntrepreneurshipMSc Data Science with Work PlacementMSc Global Business Management with Work Placement MBA with Work PlacementMS in Robotics and Autonomous SystemsMS in Civil EngineeringMS in Internet of ThingsMSc International Logistics and Supply Chain ManagementMBA- Business InformaticsMSc International ManagementMBA in Strategic Data Driven ManagementMSc Digital MarketingMBA Business and MarketingMaster of Business AdministrationMSc in Sustainable Global Supply Chain ManagementMSc Digital Business Analytics MSc in International HospitalityMSc Luxury and Innovation ManagementMaster of Business Administration-International Business ManagementMS in Computer EngineeringMS in Industrial and Systems EngineeringMSc International Business ManagementMaster in ManagementMSc MarketingMSc Business ManagementMSc Global Supply Chain ManagementMS in Information Systems and Technology with Business Intelligence and Analytics ConcentrationMSc Corporate FinanceMSc Data Analytics for BusinessMaster of Business AdministrationMaster of Business AdministrationMaster of Business AdministrationMSc in International FinanceMSc in International Management and Global LeadershipMaster of Business AdministrationBachelor of BusinessMaster of Business Administration 60 ECTSMaster of Business Administration 90 ECTSMaster of Business Administration 90 ECTSBachelor of Business AnalyticsBachelor of Information TechnologyMaster of Business AdministrationMBA Business AnalyticsMSc in Marketing Analytics and Data IntelligenceMS Biotechnology Management and EntrepreneurshipMSc in Luxury and Fashion ManagementMaster of Business Administration (90 ECTS)Bachelor of Business Administration (180 ECTS)B.Sc. Computer Science (180 ECTS) MSc in International Corporate Finance MSc in Sustainable Luxury and Creative IndustriesMSc Digital Marketing
For College Students

Introduction to Callbacks Function in Javascript

$$/$$

Before looking at callbacks, let us first look at how you can pass a function as an argument to another function.

$$/$$

In the last video, you looked at how you can pass another function as an argument and then how you can call this passed function.
 

$$/$$

 

In the next video, you’ll learn from Srishti about callbacks.

$$/$$

A callback is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of action.


Now, depending upon your usage, you can define this function to be called after some other task has finished its execution.
 

Callbacks serve a great purpose in asynchronous programming in the following two cases:
1. Mark completion of a long-running task:

Suppose you have a long-running task and you cannot decide how much time it will take to finish its execution. In such a case, you can use a callback which will be invoked when the long-running task has finished its execution, thus notifying its completion.
 

2. Execute a task after a long-running task:

Suppose you have a long-running task and you cannot decide how much time it will take to finish its execution but you know that you need to perform some other task whenever the long-running function finishes its execution. In such a case, you can use a callback which contains the other task to be carried out after the execution of a long-running task. Also, this callback will be invoked after the long-running task has finished.
 

In the next video with Srishti, you’ll look at some examples of the callbacks.

$$/$$

In the last video, you looked at how you can use callbacks in synchronous JavaScript.

 

The following code, which you looked at in the video, showcases a callback written as an anonymous inline function and passed as an argument to another function:

 

Code Snippet:

let add = (callback) => {
    let x = 2, y = 3;
    console.log("Sum :", x + y);
    callback();
}

add(function() {
    console.log("Finished this operation!");
});

const subtract = (callback) => {
    let x = 2, y = 3;
    console.log("Difference :", x - y);
    callback();
}

subtract(function() {
    console.log("Finished this operation!");
});

 

Output:

Sum : 5
Finished this operation!
Difference : -1
Finished this operation!

 

You also looked at how a callback can be written as an anonymous independent function and passed in as an argument to another function. The code that you looked for the same is given below:

 

Code Snippet:

let add = (callback) => {
    let x = 2, y = 3;
    console.log("Sum :", x + y);
    callback();
}

const subtract = (callback) => {
    let x = 2, y = 3;
    console.log("Difference :", x - y);
    callback();
}

const displayCompletion = () => {
    console.log("Finished this operation!");
}

add(displayCompletion);
subtract(displayCompletion);

 

Output:

Sum : 5
Finished this operation!
Difference : -1
Finished this operation!

 

With Srishti, you’ll now look at a real application of callbacks in the next video.

$$/$$

In the last video, you looked at how callbacks are an essential ingredient to the event listeners using the addEventListener() method in JavaScript, the syntax of which is given below:

Syntax:

.addEventListener('eventName', eventHandlerCallback);

 

You can simplify the above syntax as:

.addEventListener('eventName', () => {
    // code to be executed when the specified event (eventName) is fired
});