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

132
Views
react JS, async await access promise value

I have the following async function,

async function getScriptText(structure, dicto) {
        try {
            const rand = Math.floor(Math.random() * (max - min + 1)) + min;
                const response = await {text:dicto[rand].text, title:dicto[rand].title}
                return response
        } catch (error) {
            console.log("errore:",error)
        }

    }

then I use it with

            const scriptModel =  [
{script: getScriptText(props.sessionItem["induction"], iDicto)} ]

but when logged scriptModel, I see the promise not the value, how to use the returned value please?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Not sure if using async makes sense in your case, but in order to read the value from the promise, you need to either use .then() or await:

With .then():

let scriptModel;

getScriptText(
    props.sessionItem["induction"],
    iDicto
).then(result => scriptModel = {script: result});

With await:

let scriptModel;

(async () => {
    scriptModel = {
        script: await getScriptText(
            props.sessionItem["induction"],
            iDicto
        )
    };
})();
about 4 years ago · Juan Pablo Isaza 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!