Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

323
Vistas
How to await an async function in the constructor in JavaScript

I'm writing a JS class, which calls wasmFunc in the constructor and saves its result to this.val. However, because wasmFunc is from a WebAssembly file, an async function loadWasm must be called before wasmFunc is callable. So I'm trying to achieve something like this:

// index.js
async loadWasm() {
   // load wasm file
}

export class MyClass {
  constructor() {
    await loadWasm(); // Cannot work
    this.val = wasmFunc();
  }
}

export const myObj = new MyClass();

Ideally, this file index.js should export myObj that is ready for use. However, await cannot be used in the constructor.

Any solutions or thoughts?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

  • Constructors must return an instance of the class being constructed
  • Async functions must return a promise

Since a function can only do one of those things, a constructor function can't be async.

Helper Function

You can use a helper function:

// index.js
async loadWasm() {
   // load wasm file
}

export class MyClass {
  constructor(wasm) {
    this.val = wasm;
  }
}

async function myClassFactory() {
    const wasm = await loadWasm();
    const instance = new MyClass(wasm);
    return instance;
}

You'll need to await the result of calling myClassFactory().

Store a promise in the object

You could also store a promise in the object you create:

// index.js
async loadWasm() {
   // load wasm file
}

export class MyClass {
  constructor() {
    const wasmPromise = loadWasm(); // Cannot work
    this.val = wasmPromise;
  }

  async doSomethingWithValue() {
    const wasm = await this.val;
    console.log(wasm);
  }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

class MyClass {
    async constructor() {

    }
}

-> Uncaught SyntaxError: Class constructor may not be an async method

Edit: But you can this.

class MyClass {
    constructor(args) {
        this.arguments = args;
    };
    static async new(...args) {
        return new this(args);
    }
}
-> MyClass.new("test")
about 4 years ago · Juan Pablo Isaza Denunciar

0

export class MyClass {
    async loadWasm() {
        
    }

    async fn(){
        await this.loadWasm(); // Cannot work
        this.val = wasmFunc();
    }

  constructor() {
    this.fn()
  }
}

export const myObj = new MyClass();

try this. This should work for you.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda