-
Preface: I'm so new to this. Please be kind to me 🙏🏻 I am trying to simulate the tournament by using this function const proceed = async () => {
data = await manager.get.tournamentData(0);
data.match.forEach(async (match) => {
if (match.status === 0) {
return;
}
if (match.round_id === round) {
if (Math.random() > 0.5) {
await manager.update.match({
id: match.id,
opponent1: { score: 1, result: 'win' },
opponent2: { score: 0, result: 'loss' }
});
left.push(match.opponent1.id)
} else {
await manager.update.match({
id: match.id,
opponent1: { score: 0, result: 'loss' },
opponent2: { score: 1, result: 'win' }
});
left.push(match.opponent2.id)
}
}
});
} However, when I run this function, it does not proceed to the next round. Do you have any suggestions on this? Thank you in advance. Dependency
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hey! So you have 2 issues here. First, be aware that functional array methods in JS — like So what you should do instead of using await Promise.all(data.match.map(async (match) => {
// ...
await manager.update.match({ ... })
})) And second, what you are doing here is running promises in concurrence (NB: Node.js doesn't do parallel). But my storage implementations — So you should use a normal for (const match of data.match) {
if (match.status === 0)
continue;
if (match.round_id === round) {
if (Math.random() > 0.5) {
await manager.update.match({
id: match.id,
opponent1: { score: 1, result: 'win' },
opponent2: { score: 0, result: 'loss' },
});
left.push(match.opponent1.id);
} else {
await manager.update.match({
id: match.id,
opponent1: { score: 0, result: 'loss' },
opponent2: { score: 1, result: 'win' },
});
left.push(match.opponent2.id);
}
}
} |
Beta Was this translation helpful? Give feedback.
Hey! So you have 2 issues here.
First, be aware that functional array methods in JS — like
map()
,reduce()
,forEach()
— are not compatible withasync
. They won'tawait
between each iteration as you might think.So what you should do instead of using
forEach()
is tomap()
your array items to an array of promises, that can then be awaited withPromise.all()
:And second, what you are doing here is running promises in concurrence (NB: Node.js doesn't do parallel).
But my storage implementations —
brackets-json-db
,brackets-memory-db
... — do not support concurrent operations.Since you are up…