callBack function : JavaScript - coder's gyan


 


// Scenario


// 1. Register
// 2. Send welcome Email
// 3. Login
// 4. Get-user data
// 5. Display user data

// i want to give time to register function before all other function.

// Hindi me bole to callback function hm tab use karenge jab hame kisi 
// ek task ko complete hone ke bad dusra task karrna ho to ham
// call back funciton ka use karte hain


//  down below i use call back


// CALL BACK 

// function register(callback) {
//   setTimeout(() => {
//     console.log("resgister end");
//     callback();
//   }, 2000);
// }

// function sendEmail(callback) {
//   setTimeout(() => {
//     console.log("Email send");
//     callback();
//   }, 3000);
// }

// function login(callback) {
//   setTimeout(() => {
//     console.log("Logoin end");
//     callback();
//   }, 1000);
// }

// function getUserData() {
//   setTimeout(() => {
//     console.log("Got user data");
//   }, 1000);
// }

// function DisplayUserData() {
//   setTimeout(() => {
//     console.log("User data displayed");
//   }, 1000);
// }

// This is callback function :--
// call back hell

// register(function (){
//     sendEmail(function(){
//         login(function(){
//             getUserData(function(){
//                DisplayUserData();
//             });
//         });
//     });
// });

// console.log("other application work is doing fine");

//  This is not a good practise :- 
// Thats why we use promises..