Callback Functions: =================== -> Callback function is a function that is passed as an argument to another function and is executed after the completion of that function. -> Callbacks are a fundamental concept in asynchronous programming and are used to ensure that code runs in non-blocking way. Syntax: ======= function f1(f2){ } function f2(){ } function greets(name){ console.log("Hello",name); } function processUserInput(callback){ const name = "Kishore"; callback(name); } processUserInput(greets); Use cases of callback functions: ================================ 1) Asynchronous operations ========================== API Calls File reading/writing etc. 2) Event Handling ==================================================== Promises: ========= -> Promises are powerful way to handle asynchronous operations. -> A promise is an object that represents the eventual completion of an Asynchronous operation and its resulting a value. -> Each promise can define with three different states: 1) Pending: Initial state, neither fulfilled nor rejected. 2) Fulfilled: The operation is completed successfully. 3) Rejected: The Operation failed. Creating of Promise: ==================== constructor Promise() Syntax: Any Object name = new Promise((resolve, reject) => {}); let promise = new Promise((resolve, reject) => { let condition = true; if(condition){ resolve("Promise is fulfilled."); } else{ reject("Promise if rejected."); } }); =================== Handling with promise: ====================== let fetchData = new Promise((resolve,reject) => { let success = true; setTimeout(() => { if(success){ resolve("Data fetched successfully."); } else{ reject("Failed to fetch data."); } },2000); }); fetchData .then((data) => { console.log(data); }) .catch((error) => { console.log(error); }); chaining promises: ================== -> a promise is associated with a value, that value can send to another promise is called as "Chaining promise". let promiseChain = new Promise((resolve,reject) => { resolve(100); }); promiseChain .then((value) => { console.log(value); // 100 return value * 2; // 200 }) .then((value) => { console.log(value); // 200 return value * 3; // 600 }) .then((value) => { console.log(value); // 600 }) .catch((error) => { console.log("Error: "+error); });