callback functions explained version 1

PHOTO EMBED

Sun Jun 23 2024 04:23:58 GMT+0000 (Coordinated Universal Time)

Saved by @davidmchale #callback #functions

(function () {
  'use strict';

  function fetchData(callback) { // create the first (outer) function
    const data = 'get data from an API';
    setTimeout(() => {
      callback(data); // use the callback param as a function and pass in the data value
    }, 1000);
  }

  function displayData(data) { // create the inner function that takes in the callback as a param
    console.log('processing data');
    console.log(data);
  }

  fetchData(displayData); // finally call the outer function with the inner function as a value
})();
content_copyCOPY