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

159
Views
async await not working in composable function vue 3

In my project I have a function for downloading the files. When click the button the function onDownload will be called:

import {useOnDownload} from "../../use/useOnDownload"

setup() {

    ...

    const loading = ref(null)
    onDownload = (id) => {
        loading.value = id
        await useOnDownload(id)
        loading.value = null
    }
    
    return {loading, onDownload}
}

I refactored the code for api in a file useOnDownload.js call because the same code is used in another components as well.

export async function useOnDownload(id) {
    // make api call to server with axios
}

What I did wrong? I need to wait for the function useOnDownload ... in order the loader to work.

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

0

I managed to solved another way without async and await...

I passed the reference object loader to the function parameter (as optional) and handle from there...

export function useOnDownload(id, loader) {
   if(loader !== undefined) {
     loader.value = id
   }
   axios.post('/api/download', {id: id}, {
      responseType: 'blob'
   }).then(response => {
     // handle the response
     ...
     if(loader !== undefined) {
       loader.value = null
     }
   }).catch(error => {
     // handle the error
     ...
     if(loader !== undefined) {
       loader.value = null
     }
   })
}
about 4 years ago · Juan Pablo Isaza Report

0

onDownload must be async in order to use await within it

about 4 years ago · Juan Pablo Isaza Report

0

You are using the await keyword in your onDownlaod function, but the function is not asynchronous. Here's how you should update it.

   // next line important
   onDownload = async(id) => {
    loading.value = id
    await useOnDownload(id)
    loading.value = null
}
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!