javascript - Await 和 Async 可以等待计算完成吗?
问题描述
我正在尝试使用 HTML 在 JS 中创建类似于 Monopoly 的棋盘游戏。问题是,要发生多个事件,严格来说是在一个玩家回合中一个接一个地发生。我有一个名为 takeTurn() 的函数,所有事件都在其中发生。现在我没有对话,只有基本的玩家移动,它接受一个随机数。因此,就目前的状态而言,游戏可以以闪电般的速度进行。有什么方法可以等待函数完成吗?Async 和 Await 有严格的等待时间,我想要一些实际等待函数计算完成的东西,并返回一个值?我怎么可能这样做。
async function takeTurn(who=1) {
// Move the player on the board.
calcPos(who);
// Display the player's money.
displayBills(who);
// Change player heading.
document.getElementById('playerturn').innerText = "Player " + who + "'s Turn";
// Implement getting out of jail!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// Roll and move.
// TRY TO GET OUT OF JAIL.
if (playerdata[who].pos == "jail") {
console.log(`Player ${who} rolls to attempt to get out of jail.`);
let roll1 = rollDice();
let roll2 = rollDice();
// The player attempts to role doubles and get out of jail.
if (roll1 == roll2) {
console.log(`Player ${who} rolled doubles and got out of jail.`);
move(who, roll1+roll2);
endTurn(who);
} else {
console.log(`Player ${who} failed to roll doubles and is still stuck in jail.`);
endTurn(who);
}
// Move if we aren't in jail.
} else {
// Roll the dice.
console.log(`Player ${who} rolls to move.`);
let roll1 = rollDice();
let roll2 = rollDice();
console.log(`Player ${who} rolls a ${roll1} and a ${roll2}. They move ${roll1+roll2} spaces.`);
move(who, roll1+roll2);
// End the turn.
endTurn(who);
}
}
解决方案
试试这个代码:
async function takeTurn(who=1) {
// Move the player on the board.
await calcPos(who);
// Display the player's money.
await displayBills(who);
// Change player heading.
document.getElementById('playerturn').innerText = "Player " + who + "'s Turn";
// Implement getting out of jail!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// Roll and move.
// TRY TO GET OUT OF JAIL.
if (playerdata[who].pos == "jail") {
console.log(`Player ${who} rolls to attempt to get out of jail.`);
let roll1 = await rollDice();
let roll2 = await rollDice();
// The player attempts to role doubles and get out of jail.
if (roll1 == roll2) {
console.log(`Player ${who} rolled doubles and got out of jail.`);
await move(who, roll1+roll2);
await endTurn(who);
} else {
console.log(`Player ${who} failed to roll doubles and is still stuck in jail.`);
await endTurn(who);
}
// Move if we aren't in jail.
} else {
// Roll the dice.
console.log(`Player ${who} rolls to move.`);
let roll1 = await rollDice();
let roll2 = await rollDice();
console.log(`Player ${who} rolls a ${roll1} and a ${roll2}. They move ${roll1+roll2} spaces.`);
move(who, roll1+roll2);
// End the turn.
await endTurn(who);
}
}
rollDice
如果不需要很长时间执行,这将等待每个函数完成。
推荐阅读
- powershell - powershell - 我的代码的优化适用于一些 csv 文件,但对于 1000 个文件来说非常慢
- android-studio - 如何将在 WSL2 中运行的 Android Studio 与连接的设备或在主机上运行的 android 模拟器连接
- java - 如何替换json字符串中不正确的未转义双引号
- python - 如何使索引作业到下一个值的位置?
- java - 使用 Java 从 Apache Zookeeper 获取包含数据的所有 znode
- c# - .NET Core API:在 Linux VM 上运行的 docker 容器上升级到 EF Core 3.1 时列名“__roleName_0”无效
- docker - 在 Linux 机器中启动 minikube 需要使用 Google Container Registry 进行身份验证,为什么?
- reactjs - Fullcalendar timeGridWeek 和 timeGridDay 未正确加载
- react-native - 使用 React-navigation V5 编辑底部选项卡导航上的中间按钮
- reactjs - react-country-region-selector 组件给定错误文本字符串必须在
零件