Skip to content
Advertisement

Javascript – forEach with promise.all not working

I tried to handle promises with for each but not working

I assume it will log something because of console.log(result).

Why is it not working?

It only logs

All done (54) [ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ, ƒ]
          let promises = [];
                  mkt.forEach(async function (marketItem, index) {
                    promises.push(() => {
                      context.program.account.chain
                        .fetch(sth)
                        .then((result) => {
                console.log(result)
                        });
                    });
                  });
          Promise.all(promises)
                    .then(results => {
                      console.log('All done', results);
                    })
                    .catch(e => {
                      // Handle errors here
                    });

Advertisement

Answer

You can try this,

let promises = mkt.map((marketItem, index) => {
     return context.program.account.chain.fetch(sth)
 });
Promise.all(promises).then(results => {
    console.log('All done', results);
})
.catch(e => {
    // Handle errors here
});

Don’t push resolved promise, push the promise.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement