1

I have two different version of code but I am having trouble understanding why the second version is not working correctly. I think this is a "context" problem, but I don't understand.

In this version I get the response

// Fist version (it works)
methods: {
      async sendDatas() {
            await this.$axios({
                method: 'post',
                url: '/url',
                data: {
                    email: this.email,
                },
            })
                .then((response) => {
                    console.log(response)
                })
                .catch((error) => {
                    console.log(error)
                })
        },

In this version i can't get the response data in callApi function

sendDatas() {
            this.callApi(this.email)
                .then((response) => {
                    // Here "response" is "undefined"
                    console.log(response)
                })
                .catch((error) => {
                    console.log(error)
                })
        },

        async callApi(email) {
            await this.$axios({
                method: 'post',
                url: '/url',
                data: {
                    email,
                },
            })
                .then((response) => {
                    // Here "response" is ok"
                    console.log(response)
                })
                .catch((error) => {
                    console.log(error)
                })
        },

The async callApi function returns a promise, why can't I get the content of the response in the sendDatas function?

Thanks for your help :)

2 Answers 2

2

Your function callApi does not return the promise. You need to return it:

    sendDatas() {
        this.callApi(this.email)
            .then((response) => {
                // Here "response" is "undefined"
                console.log(response)
            })
            .catch((error) => {
                console.log(error)
            })
    },

    callApi(email) {
        return this.$axios({ // Note the return here
            method: 'post',
            url: '/url',
            data: {
                email,
            },
        })
            .then((response) => {
                // Here "response" is ok"
                console.log(response)
            })
            .catch((error) => {
                console.log(error)
            })
    },

Also, note that I removed the await and async keywords, which are not required since you're already using then and catch to handle the result.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes it's good :) Why remove the keyword "async", I thought it was essential to declare an asynchronous request. Then we always get a result in the form of a promise. @papillon
@BibDev the only use of the async keyword, as far as i know, is to enable using the await keyword inside of it. If you don't need to use await, then u probably don't need to use async either. Also, an async function always return a promise. If it is supposed to return a value other than a promise, then it returns it as a resolved promise, which explains why you could use .then on the result of your function even though you had not explicitely returned a promise from it. If u wanna learn more I suggest reading this doc which is really well explained: javascript.info/async-await
1

I'm pretty sure you are returning the promise twice. Can you try:

sendDatas() {
            this.callApi(this.email)
                .then((response) => {
                    // Here "response" is "undefined"
                    console.log(response)
                })
                .catch((error) => {
                    console.log(error)
                })
        },

        async callApi(email) {
            // Removed the `then`
            return this.$axios({
                method: 'post',
                url: '/url',
                data: {
                    email,
                },
            });
        },

1 Comment

Thank you ! :) It work with return await this.$axios({

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.