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

241
Visualizações
How to use the result of .then function to another function in js?

Please help I want to use the result of function 1 (Fn1) in function 2 (Fn2).

App={
st: null,//st is number value

Fn1: function() {
    App.contracts.contractName.deployed().then(function(instance){
       return instance.getST();
    }).then(function(result){      
        App.st = result;    
    });
},
Fn2: function() {
       alert(App.st)//
}    
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need to call Fn1 before Fn2 to access it's value, so let's wrap Fn1 into Promise:

App = {
    st: null,//st is number value

    Fn1: function() {
        return new Promise((resolve, reject) => {
            App.contracts.contractName.deployed().then(function(instance){
                return instance.getST();
            }).then(function(result){      
                App.st = result;
                resolve();
            }).catch(function(err){
                reject(err);
            })
        })
    },
    Fn2: function() {
        alert(App.st)
    }    
}

or better with async/await:

App = {
    st: null,//st is number value

    Fn1: async function() {
        try {
            const instance = await App.contracts.contractName.deployed();
            const result = await instance.getST();
            App.st = result;
        } catch(err) {
            throw err;
        }
    },
    Fn2: function() {
        alert(App.st)
    }    
}

Now you can wait until Fn1 exec before calling Fn2:

App.Fn1().then(function() {
  App.Fn2()
})

or using async/await:

await App.Fn1()
App.Fn2()
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