Tengo un archivo que exporta una función asíncrona, como esta
// api.js import { getInfo } from "somewhere"; export async function getSomething() { await getInfo() }y luego intento importarlo a otro archivo como este.
// Dashboard.vue import { getSomething } from "./api.js"; async created() { await getSomething() },pero me sale este error
Syntax Error: Unexpected reserved word 'await'. (77:26) 77 | await getSomething()Por supuesto, todo esto es pseudocódigo, pero el problema es: ¿por qué mi archivo no reconoce el método importado como una llamada asíncrona? ¿Hay alguna forma diferente de importar algo cuando se trata de un método asíncrono?
editar: tal vez mi código no brinda suficiente contexto, así que aquí hay un mejor ejemplo de cómo estoy consumiendo el método asíncrono
<template> <div>Hello world</div> </template> <script> import { getSomething } from "./api"; export default { methods: { async init() { await getSomething() } }, async created() { await this.init() } }; </script> aquí hay una vista exacta de la falla de compilación. getAssessment === getSomething en mi ejemplo anterior
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 | }Prueba esto:
// 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();