Using Then() On A Failed Promise?
Solution 1:
You need to throw
the error caught in the catch
block of the store
function
async store() {
try {
return await axios.post('/upload', data);
} catch (error) {
throw error;
}
}
You could also skip catching the error in the store
function and simply catch it when store
function is called. To do this, you just need to return the result of axios.post(...)
.
async store() {
return axios.post('/upload', data);
}
(Note that, without the try-catch
block, you don't need an await
before axios.post(...)
because the promise returned by the store
function will be resolved to the promise returned by axios.post(...)
. This means that if the promise returned by axios.post(...)
is fulfilled, promise returned by the store
function will also fulfil with the same fulfilment value with which the promise returned by axios.post(...)
fulfilled.)
It is uncommon to pass second argument to then
function. Instead, you should chain a .catch()
block to catch the error.
store()
.then(() => console.log('ok'))
.catch(() => console.log('not ok'));
Post a Comment for "Using Then() On A Failed Promise?"