首页 > 解决方案 > 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);
    }
}

标签: javascript

解决方案


试试这个代码:

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如果不需要很长时间执行,这将等待每个函数完成。


推荐阅读