Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

185
Vistas
setTimeout not waiting in class constructor

I am trying to set a timeout in a class constructor, to turn of the emitter, but when calling new Collector(options) it doesn't wait and immediately runs the .stop() function, which it should only do after 15000 ms.

My code:

Collector.js:

module.exports = class Collector extends EventEmitter {
    #counter;
    #ended;
    #timeout;
    #timeoutTime;
    /**
     * 
     * @param {{ filter?: CollectorFilter, max?: number, type: string, time?: number}} options 
     */
    constructor(options, client) {
        super();
        this.#counter = 0;
        this.collected = new Collection();
        this.filter = options.filter ?? (() => true);
        client.collectors.set(options.type, this);
        this.max = options.max ?? 1;
        this.#ended = false;
        this.#timeoutTime = options.time ?? 15000;
        console.log(this.#timeoutTime)
        this.#timeout = setTimeout(() => {
            console.log('hey')
            this.stop('TIME_END')
        }, this.#timeout);
    }
    /**
     * 
     * @param  {MessageComponentInteraction | ModalInteraction} interaction 
     */
    collect(interaction) {
        if(this.#ended) return;
        const filterBoolean = this.filter(interaction)
        if(filterBoolean) {
            this.#counter++
            this.collected.set(interaction.id, interaction)
            this.emit('collect', interaction);
            if(this.#counter === this.max) {
                this.emit('end', this.collected, 'MAX');
                this.#ended = true;
            }
        }
        else {
            this.emit('ignore', interaction)
        }
    }

    stop(reason = 'user') {
        this.#ended = true;
        clearTimeout(this.#timeout);
        this.emit('end', this.collected, reason)
    }
    
}

Calling the constructor:

const collector = new Collector({ filter: (i) => i.customId === 'hey',  max: 2}, client, );
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda