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

109
Vistas
Calculate Value from JS Promise

I have assigned a callback function to a variable. The function then returns a promise stating it is fulfilled and the value. I want to be able to return the value and use it to perform a mathematical calculation.

Javascript code:

const DollarValue = web3.eth.getBalance(address, (err, balance) =>{
    const EthValue =  web3.utils.fromWei(balance, 'ether')
    TotalEth = parseFloat(EthValue) * 4000;
    return TotalEth;
  
})

console.log(DollarValue);

In the console I get the below output.

Promise { <state>: "pending" }
​
<state>: "fulfilled"
​
<value>: "338334846022531269"
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Assuming this is the interface you're using, this is an asynchronous interface and thus you cannot directly return the value from the function or its callback as the function will return long before the value is available. You have two choices. Either use the balance or TotalEth value that you calculate from it inside the callback or skip the callback entirely and use the promise it returns.

With the plain callback:

web3.eth.getBalance(address, (err, balance) => {
    if (err) {
        console.log(err);
        // do something here upon error
        return;
    }
    const EthValue =  web3.utils.fromWei(balance, 'ether')
    const TotalEth = parseFloat(EthValue) * 4000;
    console.log(TotalEth);
    
    // use TotalEth here, not outside of the callback
  
});

Using the returned promise:

web3.eth.getBalance(address).then(balance => {
    const EthValue =  web3.utils.fromWei(balance, 'ether')
    const TotalEth = parseFloat(EthValue) * 4000;
    
    console.log(TotalEth);
    
    // use TotalEth here, not outside of the callback
}).catch(e => {
    console.log(e);
    // handle error here
});

Or, using await with the promise:

async function someFunction() {

    try {
        const balance = await web3.eth.getBalance(address);
        const EthValue =  web3.utils.fromWei(balance, 'ether')
        const TotalEth = parseFloat(EthValue) * 4000;
        
        console.log(TotalEth);
        
        // use TotalEth here, not outside of the callback
    } catch(e) {
        console.log(e);
        // handle error here
    }
}
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