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

215
Vistas
How to create wrapper around asynchronous loaded library

I'm using React and try to import some library that requires me to await it before I can start to use it. Something like this:

import theLibrary from ...

const myApp = () => {
  (async () => {
    await theLibrary.start
    const library = theLibrary
    const bla = library.methods.genBla()
    // ...
  })()

  // ...
}

I use this library somewhat often and I want to make a wrapper around it, so I won't get any delay on waiting for it to start all the time, but I don't know how I can achieve it. I would like to know what are the possible ways(OOP/FP) to do it and what is the best one in your opinion.

I want(perhaps you have better alternative?) to use it like this in my components:

import libWrapper from ...

const myApp = () => {
  const bla = libWrapper.methods.genBla()
  // ...
}
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You could use a hook like:

async function getBlaLib() {
    await theLibrary.start
    const library = theLibrary
    const bla = library.methods.genBla()
    return bla
}

function useBlaLib() {
    const [bla, setBla] = React.useState();
    const [loading, setLoading] = React.useState(false);
    React.useEffect(() => {
      setLoading(true)
      getBlaLib().then(bla => {
          setBla(bla)
          setLoading(false)
     })
    }, [])
    return [bla, loading]
}

Here is a more complete example: https://codesandbox.io/s/friendly-smoke-sguk21

about 4 years ago · Santiago Trujillo 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