简体   繁体   中英

How to nest promises with truffle deployer in truffle migration script

I am using truffle deployer to deploy my solidity contracts:

module.exports = function(deployer, network) {
  ...
}

I would like to store some data on the chain within this migration process. Basic storing of data is working properly by using the promise callback parameter and call some functions on the contract. But I need to do something more complex like explained in the following code snippet:

deployer.deploy(A).then(instance => {
  instance.addB(id, some params ...).then(result => {
    // result is not the added B -> using getB() to load B
    
    instance.getB(id).then(instanceB => {
      instanceB.addC(id, some params ...);
    })
  });
})

The problem is, that the inner functions are not executed properly. instance.addB() does properly store B into the chain. But C is never stored to the chain and I don't get why. Also if I add console.log('some text') to the inner function it's not printed to the console.

Does someone know how to resolve this issue?

Use "await/async" instead of the promise callbacks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM