can someone explain, why Promise fires then() function (and also catch() function) when reject is called?
When resolve is called, only then() is trigger - OK
When reject is called, both then() and catch() function are called - Problem
static logIn(email, password) {
async function getSession () {
let data = new FormData();
data.append('email', email);
data.append('password', password);
const response = await fetch(
url,
{
method: 'POST',
mode: 'cors',
body: data,
cache: 'no-cache',
headers: {
'Accept': 'application/json',
},
}
);
const json = await response.json();
return json;
}
return new Promise((resolve, reject) => {
getSession()
.then(json => {
if (json.status === 'ok') {
resolve('OK');
} else {
reject('LogIn failed.');
}
})
.catch(error => reject('LogIn failed.'))
});
};
logIn()
.then(console.log('logged in'))
.catch(error => console.log('not logged in'));