Answer the question
In order to leave comments, you need to log in
Help with JS async?
I'm making a mini-game, there is a character's health, by pressing "Q" 10 is taken away from him using the damage function.
We need to return something to the fight function so that at the moment when the health is below 0, the message "finish"
is displayed in the console .
The fight function should finish its execution when the health is 0
let health = 50
async function fight(){
addKeysEvents();
return new Promise((resolve) =>
})
}
function damage(){
health = health -10;
}
function addKeysEvents(){
document.addEventListener('keydown', function(event){
if (event.code == 'KeyQ'){
damage();
}
})
}
fight()
Answer the question
In order to leave comments, you need to log in
if (health <= 0){
console.log("финиш бл*");
}
function damage(){
health -= 10;
}
function addKeysEvents(){
document.addEventListener('keydown', function(event){
if (event.key == 'Q'){
damage();
}
})
}
Promise calls resolve on success and reject on failure.
In your case, most likely so, but the architecture of the code is very strange
async function fight(){
addKeysEvents();
return new Promise((resolve) =>
health <= 0 && resolve('finish')
})
}
fight().then( v => { console.log(v) })
montakarlo , Let's try to think.
You have a piece of code in the fight function
async function fight(){
addKeysEvents();
return new Promise((resolve) =>
})
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question