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?
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
)
};
})();