0

I am trying to make an axios request but still receiving the wrong return value.

The axios method in on one file:

    export const sendFormSuccessfully = data => {
    let error = '';
   axios.post('example url', {data})
        .then(() => {
          error = '';
        })
        .catch(reason => {
           error = reason;
        });
      return error;
    };

I am calling the method from a different file:

 submittedForm = data => {
    const resultSubmission = sendFormSuccessfully(data);
    console.log(resultSubmission);
  };

But everytime I receive an empty string. I understand it is because it is async function but I thought that because axios returns promises the value should be correct.

Zotov
  • 265
  • 1
  • 8
  • 20
  • you are right about async part, try using submittedForm = async (data) => { const resultSubmission = await sendFormSuccessfully(data); – Luv Jul 23 '19 at 09:40
  • I am getting error: Uncaught ReferenceError: regeneratorRuntime is not defined. – Zotov Jul 23 '19 at 09:43
  • your return statement fires before the `then`/`catch` execute. You can use `async`/`await` as suggested (but you have to add the await, or return the promise and resolve it from within the `then`/`catch` section. – Yaelet Jul 23 '19 at 09:47

0 Answers0