1

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)//
}    
}
1
  • Where and when are you calling App.Fn2()? Commented Sep 1, 2022 at 7:42

2 Answers 2

2

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()
Sign up to request clarification or add additional context in comments.

3 Comments

Warning: This is the nested promise anti-pattern. App.contracts.contractName.deployed() already returns a promise - there is no point in creating another one.
@Quentin that's why I suggest OP to use async/await where it's possible instead of old fashioned promise chaining/nesting
@Xeelley thanks, Both methods work.
0

You can just return the Promise defined in Fn1

Fn1: function() {
  return App.contracts.contractName.deployed().then((instance) => {
    return instance.getST();
  }).then((result) => {
    // Note that we return the assignment
    return App.st = result;
  });
}

Then you have two options, either you can call Fn1 before Fn2

App.Fn1().then(st => App.Fn2());

Or you can adjust Fn2's implementation to call Fn1 first.

// simplistic solution
Fn2: function() {
  App.Fn1().then(st => {
    // do something with st
  });
}

// more robust solution
Fn2: function() {
  const resolveST = () => App.st != null ? Promise.resolve(App.st) : App.Fn1();
  resolveST().then(st => {
    // do something with st
  })
}

Then using Fn2 is as simple as App.Fn2()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.