Search code examples
javascriptoopprototypeprototypal-inheritance

Waiting for other function to end in constructor function


I have this type of constructor of Child. Function play has to decrement hunger and increment fun after every second for the time, that is put as a parameter. If either the fun is 10 or the hunger is 1, it has to stop. Function need has to return CURRENT fun and hunger. I have a problem how to do it if I want to wait for play() if there is any pending, like in my code. Could you help? Parameter in play determines how many seconds the child plays - so e.g. if there is 3, the fun will increase +3

function Person(firstName, lastName, age, type, hunger) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
    this.type = type;
    this.hunger = hunger;
}

function Child(firstName, lastName, age, type, hunger) {
    Person.call(this, firstName, lastName, age, type, hunger)
    this.fun = 5
}
Child.prototype = Object.create(Person.prototype)
    Child.prototype.constructor = Child

    Child.prototype.play = async function (sec) {

    const interval = setInterval(() => {
        if (this.fun >= 10 || this.hunger <= 1) {
            clearInterval(interval)
        } else {
            this.fun += 1
            this.hunger -= 1
        }
    }, 1000)

        setTimeout(() => clearInterval(interval), sec * 1000)
}
Child.prototype.need = async function () {
    await this.play()
    return "Fun: " + this.fun + " , hunger: " + this.hunger
}

const child1 = new Child("child1", "childdddd", 3, 'boy', 4)

child1.play(3)

console.log(child1.need())


Solution

  • Your code works perfectly, but the async functions return a Promise, so you have to either:

    • Provide a callback in then
    • await it

    function Person(firstName, lastName, age, type, hunger) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
        this.type = type;
        this.hunger = hunger;
    }
    
    function Child(firstName, lastName, age, type, hunger) {
        Person.call(this, firstName, lastName, age, type, hunger)
        this.fun = 5
    }
    Child.prototype = Object.create(Person.prototype)
    Child.prototype.constructor = Child
    
    Child.prototype.play = function (sec) {
        this.result = new Promise(resolve => {
            const interval = setInterval(() => {
                if (this.fun >= 10 || this.hunger <= 1) {
                    end();
                } else {
                    this.fun += 1
                    this.hunger -= 1
                }
            }, 1000)
            
            setTimeout(end, sec * 1000)
            
            function end() {
                clearInterval(interval)
                resolve()
            }
        })
    }
    Child.prototype.need = async function () {
        await this.result;
        return "Fun: " + this.fun + " , hunger: " + this.hunger
    }
    
    const child1 = new Child("child1", "childdddd", 3, 'boy', 4)
    
    child1.play(3)
    
    child1.need().then(console.log);