Javascript 可以等待和异步等待计算完成吗?

Javascript 可以等待和异步等待计算完成吗?,javascript,Javascript,我正在尝试使用HTML创建一个类似于JS中垄断的棋盘游戏。问题是,在一个玩家回合中,多个事件会发生,严格地说是一个接一个。我有一个名为takeTurn()的函数,其中所有事件都发生。现在我没有对话,只有基本的玩家动作,这是一个随机数。因此,从目前的状态来看,游戏可以以闪电般的速度进行。有什么方法可以让我等待函数完成吗?Async和Await有一个严格的等待时间,我想要一些实际等待函数计算完成的东西,以及返回的值?我该怎么做呢 async function takeTurn(who=1) {

我正在尝试使用HTML创建一个类似于JS中垄断的棋盘游戏。问题是,在一个玩家回合中,多个事件会发生,严格地说是一个接一个。我有一个名为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
不需要很长时间,这将等待每个函数完成。

异步/wait等待严格的时间是什么意思?您要等待哪个函数?很难判断takeTurn中的哪些调用是同步的,哪些是异步的
wait
可以无限期地等待。我想让它等待玩家掷骰子,这目前只需要几分之一秒,不会产生明显的差异,但最终会有动画播放。我还想让它等待玩家完成移动()这不是
wait
本身的候选,而是模型的候选。