带有promise.all的Javascript-forEach不起作用



我试着用为每个人处理承诺,但不起作用

我想它会因为console.log(result)而记录一些东西

为什么它不起作用?

它只记录

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
});

你可以试试这个,

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
});

不要违背坚定的承诺,要信守承诺。

最新更新