[Vuejs]-The promise code never work as I expect. What is the mistake in my code?

1πŸ‘

βœ…

I think you are missing the .then on your axios requests. Here is some example:

const submitNewCategory = () => {
    return new Promise((resolve) => {
        axios.post('/api/categories/store', {
            title: newCategory.value
        }).then(response => {
            resolve(response);
        })
    })
}

Using the .then you are waiting for the request to be completed.

–Edit–
You can also add a second function to your .then for errors

const submitNewCategory = () => {
    return new Promise((resolve, reject) => {
        axios.post('/api/categories/store', {
            title: newCategory.value
        }).then(response => {
            resolve(response);
        },
        error => {
            // Can add other treatments for errors
            reject(error);
        })
    })
}

Leave a comment