This comes in handy when a callback function returns 2 things: undefined or Promise. This happens when:
- Callback may return
undefinedwhen confirmation dialog (window.confirm) is cancelled. - Otherwise, just proceeds, and finally callback returns a Promise which does the ajax request
function callback(id) {
if (!confirm('Are you sure to delete?')) {
return;
}
return axios.delete(`/products/${id}`);
}Promise.resolve(promise)
.then(res => {
return !res
})
.then(res => {
// Do something
});resolve(promise)
.then(res => {
// Do something
});Careful, if you don't return something inside a then, you're going to waste a lot of time why your program's acting weird.
// bad
function callback(id) {
if (!confirm('Are you sure to delete?')) {
return;
}
return axios.delete(`/products/${id}`)
.then(res => {
// do something
// not return
});
}
// good
function callback(id) {
if (!confirm('Are you sure to delete?')) {
return;
}
return axios.delete(`/products/${id}`)
.then(res => {
// do something
return res;
});
}