// 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.
// promises is modern way to execute callback
// PROMISES
// now i am getting error in first step
// how to handle error in this situation (line no. 25)
// to tackle this situation we can use catch (line no. 75)
// again i am getting error in email send step (line no. 34)
// to tackle this situation we use catch in async await meth.. (line no.98)
// now we handling error using try catch (line no. 90)
function register() {
return new Promise(function(resolve , reject){
setTimeout(() => {
console.log("resgister end");
resolve();
// reject("error while registering");
}, 2000);
});
}
function sendEmail() {
return new Promise(function(resolve,reject){
setTimeout(() => {
return reject("Error while sending email");
console.log("Email send");
}, 3000);
})
}
function login() {
return new Promise(function(resolve, reject){
setTimeout(() => {
console.log("Login end");
resolve();
}, 1000);
})
}
function getUserData() {
return new Promise(function(resolve,reject){
setTimeout(()=>{
console.log("Got user Data");
resolve();
},3000)
})
}
function DisplayUserData() {
return new Promise(function(resolve,reject){
setTimeout(()=>{
resolve();
console.log("Data displayed");
},1000)
})
}
// This is promises baby :-
// register()
// .then(sendEmail)
// .then(login)
// .then(getUserData)
// .then(DisplayUserData)
// .catch((error)=>{
// console.log("Error", error);
// })
// Now we see async and await and its better than promises
async function auth (){
try{
await register();
await sendEmail();
await login();
await getUserData();
await DisplayUserData();
} catch(err){
console.log(err);
throw new Error();
}
}
auth().then(function(){
console.log('All set');
})
.catch(function(err){
console.log(err);
})
// Other funcionality in apps is doing good
console.log("other application work is doing fine");