I'm currently reading a post on Axios in React and I'm having trouble understanding Promises.
From my understanding, Axios is a promise-based library. But the code that I'm looking at is wrapping an Axios call in a new promise. Doesn't this double up on code?
I have a 'helpers' file in my react project with a login
function and the code from the post is as follows;
return new Promise((resolve, reject) => {
axios.post(baseUrl + 'login', formData, {
headers: {
Accept: 'application/json'
}
})
.then(response => {
const token = response.data.access_token;
localStorage.setItem('access_token', token);
resolve(response);
})
.catch(error => {
console.log(error);
reject(error);
})
})
Isn't the then
and catch
the same as resolve
and reject
Any help understanding this would be greatly appreciated.