0

The function I am accessing will emit events i need to use that event but the function is a transaction when i am accessing the event it turns as promise pending . So suggest me some methods. thanks!

Please find my code below

App.contracts.Sample.deployed().then(function(instance) {
    mainInstance = instance;
    instance.countPlayers().then(function(count) {
        console.log(count.valueOf()) // returning values 
    })

    // to access events emitted
    instance.onlineCount({}, {  
        fromBlock: 0,
        toBlock: 'latest'
    }).watch(function(error, event) {
       console.log("event---->", event) /// promise pending error
       App.render();
    })
});
  • Use await or resolve the promise. – goodvibration Feb 5 at 6:51
  • App.contracts.Sample.deployed().then(function(instance) { }) . How can i call my event with deployed instance. i can able to access a return function but not event – Prasanth A S Feb 5 at 6:54
  • You should write your question properly (including any relevant code) in the designated area, not in comments. – goodvibration Feb 5 at 6:56
0

I can give you an example of how I did something similar:

First I create a variable to hold the desired output of the event. In my case it is an address of a newly created contract:

var TargetAddress;

Then I create a listener for the event and get the address from the response of the event:

var event = instance.TargetCreated(function(error, response) {
    if (!error) {
        TargetAddress = response.args.addr;
    }else{
        console.log(error);
    }
});

You can also just "console.log()" the response to see what you get as a return.

Then I await the method invocation:

await protocol.registerMitigator(MitigatorOwner,1000,"Mitigator1",{from: TargetOwner});

And right afterwards I make my assertions as follows:

return await IActor.at(MitigatorAddress).then(async function(owner) {
assert.equal(MitigatorOwner, await owner.getOwner(), "Mitigator Address is wrong");
});

All set together it looks like this:

it("Mitigator Creation", async function() {

        var MitigatorAddress;

        return await Protocol.deployed().then(async function(instance) {          
            //Get the deployed protocol instance
            //Define the fallback for the event which gives us the address of the created mitigator
            var event = instance.MitigatorCreated(function(error, response) {
                if (!error) {
                    MitigatorAddress = response.args.addr;
                }else{
                    console.log(error);
                }
            });

            await instance.registerMitigator(MitigatorOwner,1000,"Mitigator1", {from: TargetOwner});
            return await IActor.at(MitigatorAddress).then(async function(owner) { 
                assert.equal(MitigatorOwner, await owner.getOwner(), "Mitigator address is wrong");
            });
        });
    });

Hope I could give you some inputs with this :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.