1

What is the best way to handled nested contract call with web3 1.0? I'm trying to collect all data needed and format it into something useable. It could require 2 or 3 calls though. Is there a easy way to handle it, to know when all async calls have finished?

this.contract.methods.getPlayerAnimals(this.address)
.call({ from: this.address })
.then((animals) => {
  for (var animal of animals) {
    this.contract.methods.getAnimal(animal)
    .call({ from: this.address })
    .then((animalContract) => {
      this.playerAnimals.push(fullAnimal)
    });
  }
  this.socket.emit('playerAnimals', this.playerAnimals)
});

2 Answers 2

1
var animals = await this.contract.methods.getPlayerAnimals(this.address)
  .call({ from: this.address })
for (var animal of animals) {
  var animalContract = await this.contract.methods.getAnimal(animal)
    .call({ from: this.address })
0
// Assuming we are inside async function
this.socket.emit (
  'playerAnimals',
  await Promise.all (
    (await this.contract.methods.getPlayerAnimals (this.address).
      call ({ from: this.address })).
      map (animal =>
        this.contract.methods.getAnimal (animal).
          call ({ from: this.address }))));

Your Answer

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

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