Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

298
Visualizações
How to import an async function and use await -- error Unexpected reserved word 'await'

I have a file that exports an async function, like this

// api.js


import { getInfo } from "somewhere";

export async function getSomething() {
  await getInfo()
}

and then I'm trying to import it into another file like this.

// Dashboard.vue


import { getSomething } from "./api.js";

async created() {
    await getSomething()
  },

but I'm getting this error

Syntax Error: Unexpected reserved word 'await'. (77:26)
 77 | await getSomething()

Of course, this is all pseudo-code but the problem is -- why does my file not recognize the imported method as an async call? Is there a different way to import something when it is an async method?

edit: perhaps my code is not giving enough context, so here is a better example of how I'm consuming the async method

<template>
  <div>Hello world</div>
</template>

<script>
import { getSomething } from "./api";

export default {
  methods: {
     async init() {
        await getSomething()
    }
  },
  async created() {
    await this.init()
  }
};
</script>

here is an exact view of the compile failure. getAssessment === getSomething in my example above

 ERROR  Failed to compile with 1 error                                                                                                              1:29:06 AM

 error  in ./src/components/Dashboard.vue?vue&type=script&lang=js&

Syntax Error: Unexpected reserved word 'await'. (79:26)

  77 |       if (user) {
  78 |         this.user = user;
> 79 |         this.assessment = await getAssessment();
     |                           ^
  80 |       } 
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try this:

// api.js

import { getInfo } from 'somewhere'

export const getSomething = async () => {
    return await getInfo();
}


// another file

import { getSomething } from './api.js'

const created = async () => {
  await getSomething();
}

created();
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda