JS

CALLBACK FUNCTION - A SIMPLE EXAMPLE

DO AJAX CALLS function main (target) { for (let t of target) {   fetch(t.url)   .then((res) => { return res.text(); })   .then((txt) => { t.callback(txt); });  }}

THE "MAIN" FUNCTION

01

OUTPUT TO CONSOLE  function callbackA (res) {   console.log(res); }

THE CALLBACK FUNCTIONS

02

APPEND TO HTML BODY function callbackB (res) {   document.body.innerHTML = res; }

CALLBACK - WHEN WE PASS A FUNCTION INTO A FUNCTION   main([   { url: "PAGEA", callback: callbackA },   { url: "PAGEB", callback: callbackB }  ]);

WHAT IS A CALLBACK? (A)

03

CALLBACK FUNCTION GETS CALLED INSIDE THE MAIN FUNCTION   * callbackA() will output AJAX fetch results into console.   * callbackB() will output AJAX fetch results into HTML.

WHAT IS A CALLBACK? (B)

04