/* ASYNC: To get data frm the servers in the background live a goolge typing */ /*otherFucnt(); console.log("start"); function otherFucnt() { setTimeout(() => { console.log("async"); }, 2000); } console.log("end"); */ //Callbacl and Callback HEll . onSuccess, onFailure /* console.log("start"); function login(email, password, callback) { setTimeout(() => { callback({ email: email }); }, 5000); } function loginVidoes(email, callback) { setTimeout(() => { callback(["video1", "video2"]); }, 5000); } const user = login("m90khan", "a123312", (user) => { console.log(user.email); loginVidoes(user.email, (videos) => { console.log(videos); }); }); */ // will give undefined because the function did not return anything by // the time we console the user /* Promises when we fetching data from an api , we migt not get the data instead maybe get error Promises simply the callback promise is an object which either give a result or a faailure */ // const promise = new Promise((resolve, reject) => { // setTimeout(() => { // // resolve({ user: "khan" }); // reject(new Error("user not found")); // }, 2000); // }); // function loginVidoes(email, callback) { // setTimeout(() => { // callback(["video1", "video2"]); // }, 5000); // } // promise // .then((user) => { // console.log(user); // }) // .catch((err) => { // console.log(err); // }); // Refatcor into promise function login(email, password) { return new Promise((resolve, reject) => { setTimeout(() => { resolve({ email: email }); }, 5000); }); } function loginVidoes(email) { return new Promise((resolve, reject) => { setTimeout(() => { resolve(["video1", "video2"]); }, 5000); }); } // const user = login("m90khan", "a123312", (user) => { // console.log(user.email); // loginVidoes(user.email, (videos) => { // console.log(videos); // }); // }); login("m90khan", 12345) .then((user) => { loginVidoes(user.email); }) .then((video) => console.log(video)); Promise.all([login, loginVidoes]).then((result) => console.log(result)); //ASYNC AWAIT async function displayUser() { try { const loginuser = await login("khan"); const videos = await loginVidoes(login.email); } catch (err) { console.log(err); } } displayUser();