0

i need to get value from promise, set it value to field of object and return object.

array.election[0].candidates - is array of objects.

   async getCandidates(array) {
        return await array.election[0].candidates.map(candidate => {
            let candidateInfo = candidate;
            let balance = 0;
            Promise.all([this.getBalance(candidate.address)]).then(value => {
               balance = value[0];
            });
            candidateInfo.balance = balance;
            return candidateInfo;
        });
    }

Because Promise.all - async i cant get value.

Phiter
  • 14,570
  • 14
  • 50
  • 84
J. Defenses
  • 203
  • 1
  • 2
  • 9
  • 2
    Do `let info = await Promise.all([this.getBalance(candidate.address)]); let balance = value[0];`. Or better yet, why even use `Promise.all()`? Just do `let info = await this.getBalance(candidate.address);` – c1moore Jun 13 '18 at 12:05
  • Why are you even calling `Promise.all` on an array with a single promise only? – Bergi Jun 13 '18 at 12:23
  • So, i make: `async getCandidates(array) { let x = array.election[0].candidates.map(async candidate => { let candidateInfo = candidate; candidateInfo.balance = await this.getBalance(candidate.address); return candidateInfo; }); console.log(x); return x; }` Without promise.all , but it's make array with promises: http://joxi.ru/Vrw9xlPiOgGMV2 But i need resolved it, can you help me pls? – J. Defenses Jun 13 '18 at 13:52

0 Answers0