I'm having a problem trying to break out of a promise statement when an error occurs in a catch statement.
I'm not sure if I can throw an error inside a catch statement.
The problem: The catch function isn't doing anything when I throw an error.
Expected result: For the catch statement to display an alert and break the promise chain.
The code:
if (IsEmail(email)) {
$('body').loadingModal({
position: 'auto',
text: 'Signing you in, please wait...',
color: '#fff',
opacity: '0.9',
backgroundColor: 'rgb(0,0,0)',
animation: 'doubleBounce'
});
var delay = function(ms){ return new Promise(function(r) { setTimeout(r, ms) }) };
var time = 2000;
delay(time)
.then(function() { $('body').loadingModal('animation', 'foldingCube'); return delay(time); } )
.then(function() {
firebase.auth().signInWithEmailAndPassword(email, password)
.then(function () {
var user = firebase.auth().currentUser;
uid = user.uid;
configure();
})
.catch(function(error) {
throw error;
});
})
.then(function() { $('body').loadingModal('color', 'white').loadingModal('text', 'Welcome to Dtt deliveries').loadingModal('backgroundColor', 'orange'); return delay(time); } )
.then(function() { $('body').loadingModal('hide'); return delay(time); } )
.then(function() { $('body').loadingModal('destroy') ;} )
.catch(function(error) {
alert("Database error: " + error);
});
}
else {
alert("Please enter a valid email");
return;
}