Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
Passing argument to async / await function returns "undefined"

When posting data to an API and get a response, if I hardcode the body data inside the fetch call (body: "XYZ12345") it works fine, this is an example:

            const vatValidationRequest =
                fetch(
                    '/api/vies/validateVAT.php', {
                        method: 'POST',
                        body: "XYZ12345",
                        headers: {
                            'Content-Type': 'application/text'
                        }
                    })
                .then((response) => response.text())
                .then((responseText) => {
                    return responseText;
                });


            const validateVAT = async () => {
                const viesResponse = await vatValidationRequest;
                console.log(viesResponse);
            };

            validateVAT();

However, if I try to pass the body data as an argument (body: vatNumber), the validateVAT() function returns "undefined". This is what's not working:

            const vatValidationRequest = (vatNumber) => {
                fetch(
                    '/api/vies/validateVAT.php', {
                        method: 'POST',
                        body: vatNumber,
                        headers: {
                            'Content-Type': 'application/text'
                        }
                    })
                .then((response) => response.text())
                .then((responseText) => {
                    return responseText;
                });
            }


            const validateVAT = async (vatNumber) => {
                const viesResponse = await vatValidationRequest(vatNumber);
                console.log(viesResponse);
            };

            validateVAT("XYZ12345");

Any clues about how to pass the argument to the async function? thanks!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The problem is that you are not returning the response from the method. You should do this:

            const vatValidationRequest = (vatNumber) => {
                return fetch(
                    '/api/vies/validateVAT.php', {
                        method: 'POST',
                        body: vatNumber,
                        headers: {
                            'Content-Type': 'application/text'
                        }
                    })
                .then((response) => response.text())
                .then((responseText) => {
                    return responseText;
                });
            }


            const validateVAT = async (vatNumber) => {
                const viesResponse = await vatValidationRequest(vatNumber);
                console.log(viesResponse);
            };

            validateVAT("XYZ12345");
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!