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

135
Vistas
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 Respuestas
Responde la pregunta

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