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

213
Views
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 answers
Answer question

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 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!