Advanced JavaScript Training
Callback Functions

Callback Functions

  • Callback functions are functions passed as arguments to other functions. They execute when an asynchronous task is complete.
  
    function readFileAsync(filename, callback) {
       // Simulate file reading asynchronously
       setTimeout(() => {
        const fileData = 'This is the file content.';
        callback(fileData);
      }, 1000);
    }

    readFileAsync('example.txt', (data) => {
      console.log('File content: ' + data);
    });

  
Have a doubt?
Post it here, our mentors will help you out.