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

183
Vistas
.then() is not returning a promise

I am trying to import a function from another file and while rendering in the react component, it doesn't get rendered. The value of that variable is showing undefined

index.js

const GetOwnBalance = async () => {
    if (!isInitialized) {
        await init();
    }
    // const web2 = new Web3(provider);
    nftContract.methods
    .getBalance(selectedAccount)
        .call()
        .then((balance) => {
            // console.log(`Balance of ${selectedAccount} is ${balance}`);
            console.log("Intermediate balance : " + balance);
            // return balance;
        }   
    );
};
export default GetOwnBalance;

Until this everything runs perfect, but the problem is in App.js

App.js

import React, { useState } from 'react';
import  GetOwnBalance  from './index';

function App() {
    const [balance, setBalance] = useState(0);
    function fetchBalance() {
        GetOwnBalance()
            .then(value => {
                setBalance(value); //value shows undefined ???
                console.log("Final Balance : " + value);
            }
                
            );
    }

    return (
        <div className="App">
            <p>Your balance is {balance}</p>
            <button onClick={fetchBalance}>Refresh balance</button>
        </div>
    );
}

export default App;

When I try to run this piece of code, the value value in the .then() promise shows undefined. Can someone please explain why is it so?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

It is because you are not returning anything from function GetOwnBalance.

Try adding the return keyword in return nftContract.methods....

const GetOwnBalance = async () => {
    if (!isInitialized) {
        await init();
    }
    // const web2 = new Web3(provider);
   return nftContract.methods
    .getBalance(selectedAccount)
        .call()
        .then((balance) => {
            // console.log(`Balance of ${selectedAccount} is ${balance}`);
            console.log("Intermediate balance : " + balance);
            // return balance; <-- Also uncomment this
        }   
    );
};
export default GetOwnBalance;
about 4 years ago · Juan Pablo Isaza Denunciar

0

Is the "GetOwnBalance" function located at index.js or ./Web3Client'?

Instead of "value" parameter on

 .then(value => {
            setBalance(balance); //value shows undefined ???
            console.log("Final Balance : " + balance);
        }

Try just using the hook value name:

 .then(() => {
            setBalance(value);
            console.log("Final Balance : " + value);
        })

Additionally you don't have to set "GetOwnBalance" as async if you don't use await

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