async function wont break node.js (.mjs)

68 Views Asked by At

I'm trying to pause this function using the breakTheLoop var but it wont work. This is the code if anyone could help me out `

    async afk(message) {
        var breakTheLoop = false;
        while (!breakTheLoop) {

        await new Promise(resolve => setTimeout(resolve, 500));
        this.bot.setControlState('forward', true);
        await this.bot.waitForTicks(1);
        //this.bot.setControlState('sprint', true);
        this.bot.setControlState('jump', true);
        let arm = Math.random() < 0.5 ? 'right' : 'left';
        await this.bot.swingArm(arm);
        let yaw = 2*Math.random()*Math.PI - (0.5*Math.PI);
        let pitch = Math.random()*Math.PI - (0.5*Math.PI);
        await this.bot.look(yaw,pitch,false);
        await this.bot.waitForTicks(11);
        if (message === '!afkoff') {
            return breakTheLoop = true;
        }
        
    }}

This is the function i use to call to the async function

chatLog(username, message) {
    if (!botNames.includes(username)) {
        this.log(chalk.ansi256(98)(`<${username}>`), message)
    if(message === '!afk')this.afk(), this.bot.chat("I'm AFK!");
    }
}

I'm new to js so any help would be appreciated.

Expected the loop to stop but it just keeps going i've tried multiple answers on here.

0

There are 0 best solutions below