Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何暂停进程,直到异步调用的结果返回(我一直无法正确应用异步等待规则)?_Javascript_Async Await - Fatal编程技术网

Javascript 如何暂停进程,直到异步调用的结果返回(我一直无法正确应用异步等待规则)?

Javascript 如何暂停进程,直到异步调用的结果返回(我一直无法正确应用异步等待规则)?,javascript,async-await,Javascript,Async Await,尽管有很多关于Stackoverflow异步调用的讨论,但我还没有弄清楚如何使用then或任何其他方法修复代码。 在实施头尾游戏时(我的实施不是最好的方式),我不知道如何停止该过程,直到等待的结果回来: var headOrTail = ['tail', 'head']; var player1A, player1B, player2A, player2B; var startTheGame = async function () { let playerOne = await asy

尽管有很多关于Stackoverflow异步调用的讨论,但我还没有弄清楚如何使用then或任何其他方法修复代码。 在实施头尾游戏时(我的实施不是最好的方式),我不知道如何停止该过程,直到等待的结果回来:

var headOrTail = ['tail', 'head'];
var player1A, player1B, player2A, player2B;

var startTheGame = async function () {
    let playerOne = await asyncCall1();
    let playerTwo = await asyncCall2();

/****These statements have to be checked after PlayerOne and PlayerTwo are processed, and this is where I'm stuck ******/

    if (playerOne > playerTwo) {
        console.log('Player 1 won!');
    } else if (playerOne < playerTwo) {
        console.log('Player 2 won!');
    } else {
        console.log('No one won!');
    }

}

const asyncCall1 = () => {
    return new Promise((resolve) => {
        setTimeout(() => {
            player1A = Math.round(Math.random());
            console.log('Player 1 first attempt: ' + headOrTail[player1A])
        }, 2000);

        setTimeout(() => {
            player1B = Math.round(Math.random());
            console.log('Player 1 second attempt: ' + headOrTail[player1B])
        }, 4000);

        resolve(player1A + player1B);
    })
}

const asyncCall2 = () => {
    return new Promise((resolve) => {
        setTimeout(() => {
            player2A = Math.round(Math.random());
            console.log('Player 2 first attempt: ' + headOrTail[player2A]);
        }, 2000);

        setTimeout(() => {
            player2B = Math.round(Math.random());
            console.log('Player 2 second attempt: ' + headOrTail[player2B]);
        }, 4000);

        resolve(player2A + player2B);
    })
}

startTheGame();
var headOrTail=['tail','head'];
变量player1A、player1B、player2A、player2B;
var startTheGame=异步函数(){
让playeron=wait asyncCall1();
let playerTwo=等待asyncCall2();
/****在处理PlayerOne和PlayerTwo之后,必须检查这些语句,这就是我被卡住的地方******/
如果(播放者>播放者两){
console.log('Player 1赢得!');
}否则如果(播放者<播放者两个){
console.log('Player 2赢得!');
}否则{
console.log('没人赢!');
}
}
常量asyncCall1=()=>{
返回新承诺((解决)=>{
设置超时(()=>{
player1A=Math.round(Math.random());
console.log('Player 1首次尝试:'+headOrTail[player1A])
}, 2000);
设置超时(()=>{
player1B=Math.round(Math.random());
log('Player 1秒尝试:'+headOrTail[player1B])
}, 4000);
解决(player1A+player1B);
})
}
常量asyncCall2=()=>{
返回新承诺((解决)=>{
设置超时(()=>{
player2A=Math.round(Math.random());
log('Player 2首次尝试:'+headOrTail[player2A]);
}, 2000);
设置超时(()=>{
player2B=Math.round(Math.random());
log('Player 2第二次尝试:'+headOrTail[player2B]);
}, 4000);
解析(player2A+player2B);
})
}
开始游戏();

async两个函数都将立即解析,因为
setTimeout()
会立即返回。@Pointy您是对的,这就是问题所在。非常感谢。