I'm new in javascript/typescript I write blackjack app using typescript and in one player game I have bot as second player. His script looks like this:
const botTurn = (bot : Player) =>{
while(bot.Points < 21)
{
if(bot.Points < 18)
{
hitMe();
}
else
{
var random = Math.random();
if(random < 0.5)
{
hitMe();
}
else{
break;
}
}
}
stay();
}
and hitMe looks like this:
const hitMe = () =>
{
fetch('https://deckofcardsapi.com/api/deck/' + deckId + '/draw/?count=1')
.then(response => response.json())
.then(data => {
deckLenght = data.remaining;
for(let card of data.cards)
{
var newCard = getCardData(card);
players[currentPlayer].Hand.push(newCard);
renderCard(newCard, currentPlayer);
updatePoints();
updateDeckLenght();
check();
}
});
}
So botTurn doesn't wait for hitMe to finish and my browser hangs How to fix that?