Really sorry if this has been answered, I've searched everywhere and can't find the exact problem I'm facing.
Take this as the example:
const fetchData = (email, password) => new Promise(async (resolve, reject) => {
await axios.post('https://api.something.com', {
email: email,
password: password,
},
{
headers: {
'Content-Type': 'application/json',
}
})
.then(res => {
cookie = res.headers['set-cookie'];
})
.catch(err => {
return reject('Login failed');
});
await axios.get('https://api.something.com', {
headers: {
'cookie': cookie
}
})
.then(res => {
data = res;
})
.catch(err => {
return reject('Failed to retrieve something');
});
return resolve(data);
});
If the login credentials are incorrect, the 'Login failed' reject is sent but the script will keep on running and there will be an additional error message saying that cookie isn't set. I want to completely stop the script in the first catch.
I could use throw new Error('Login failed') and that would stop the script completely but I don't feel like that's the right answer and also because it makes me wonder what else could I use to resolve the promise (for other purposes) and still don't let the script continue running.
I'm also not interested in nesting functions, to avoid promise-callback christmas tree-like hell.
Am I making sense?