Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

240
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!