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

227
Views
Javascript async function throws error when using await

I'm still beginner when it comes to JavaScript. In my vuejs3 app I have the following situation (I assume knowing this is Vue is not important):

In my Vue app I have a method like this

async LoadBasicData()
{
    this.basicData = await window.LoadBasicData();
},

It calls a function in a different JavaScript file. The method looks a bit like this:

async function LoadBasicData()
{
    return new Promise(resolve =>
    {
        let result = Object.create(null);

        let url = 'MyFancyUrl';

        axios
            .get(url)
            .then((response) =>
            {
                result.data = response.data;

                result = await LoadBuildId(result);

                resolve(result);
            })
    });
}

The LoadBuildId function is looking very similar

async function LoadBuildId(result)
{
    return new Promise(resolve =>
    {
        let url = 'MySecondFancyUrl';

        axios
            .get(url)
            .then((response) =>
            {
                result.buildId = response.data;

                resolve(result);
            })
    });
}

From my understanding of all this async/await/promise this should be the correct way of doing it, but considering that it's not working I'm pretty sure there is something I have misunderstood I'm getting the error "await is only valid in async functions and the top level bodies of modules" when calling LoadBuildId. I don't understand the error, as await is called in an async function

What am I doing wrong here? Why can't my async function call another async function? Or is there a better way?

I know there is at least one way to accomplish what I need, that is by using callbacks. I have been using it until now, but I'm not really a fan of it as it really makes the code unnecessary complicated.

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

0

You're making 2 mistakes

  • Wrapping the existing promise-related functionality with your own promise (see: What is the explicit promise construction antipattern and how do I avoid it?)
  • Mixing async/await with then

The code can be hugely simplified:

async function LoadBuildId(result)
{
    let url = 'MySecondFancyUrl';
    const response = await axios.get(url)
    result.buildId = response.data;
    return result;
}

and

async function LoadBasicData()
{
    let url = 'MyFancyUrl';
    let result = Object.create(null);

    const response = await axios.get(url);
    result = await LoadBuildId(result);
    return result;
}

To be honest, it can be simplified even further - theres no need to be constructing result objects just to set properties on it - you may as well just return the results from a call to axios.get

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!