Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

179
Visualizações
.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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda