I have been using axios to make API calls using the following pattern:
axios.get('/user?ID=12345')
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
In a codebase I am working on, the pattern for API calls looks like this:
export async function getUser(...) {
return new Promise((resolve, reject) => {
customAxios.get("...", { params })
.then(response => ....).then(resolve).catch(reject))
.catch(error =>
return new reject(...));
});
}
Where customAxios looks like:
const customAxios = axios.create({...});
customAxios.interceptors.response.use(.....);
I haven't used Promises before, but what is the point of wrapping the axios call in a promise as oppose to style the axios documention has?