How do I expect a method to terminate in JS?

0

Hello,

Dude, I can not wait for the method to finish before showing the return, can anyone help me?

getValor(jogador) {
        const id_grupo = this.props.grupoSelected.id;
        if (jogador.financeiro) {
            const arrayFinanceiro = Object.keys(jogador.financeiro);
            if (arrayFinanceiro) {
                arrayFinanceiro.forEach((grupoKey) => {
                    if (grupoKey === id_grupo) {
                        return jogador.financeiro[grupoKey];
                    }
                });
            } else return 0;
        } else return 0;
    }

React native:

<Text>{ this.getValor(jogador) }</Text>
    
asked by anonymous 24.11.2018 / 20:10

1 answer

0

Javascript is asynchronous. To make your method synchronous you have to use async / await. See the example

 async function getUserFullData(){ 
    var userData = await getUser(); 
    var userAddress = await getUserAddr(userData); 
    console.log(userData, userAddress); 
}

getUserFullData(); 

Take a look at the documentation too link

    
29.11.2018 / 13:14