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

150
Vistas
How to avoid the creation of a new promise for each click?

I have a click that calls the method:

public clickEvent() {
    this.createIframe().then((iframe) => { // Return iframe or create if is not before needed inside 
      // Async hard logic here
    })
}

Problem is when user clicks a lot of times clickEvent() it fires promise and then fires a hard logic inside.

How to avoid click until logic inside is not finished? Or disable to call logic inside if it is done?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Make createIframe cache its Promise (like as an instance property), and return that first if it exists, instead of starting another. For example:

// example function that creates the Promise
const createPromise = () => {
  console.log('creating Promise');
  return new Promise(resolve => setTimeout(resolve, 3000));
}

class SomeClass {
  createIframe() {
    if (this.iframePromise) return this.iframePromise;
    this.iframePromise = createPromise();
    return this.iframePromise;
  }
  clickEvent() {
    this.createIframe().then((iframe) => {
      console.log('clickEvent has received the Promise and is now running more code');
    })
  }
}

const s = new SomeClass();
button.onclick = () => s.clickEvent();
<button id="button">click to call clickEvent</button>

If you also want to prevent // Async hard logic here from running multiple times after multiple clicks, assign something to the instance inside clickEvent instead.

// example function that creates the Promise
const createPromise = () => {
  console.log('creating Promise');
  return new Promise(resolve => setTimeout(resolve, 3000));
}

class SomeClass {
  createIframe() {
    return createPromise();
  }
  clickEvent() {
    if (this.hasClicked) return;
    this.hasClicked = true;
    this.createIframe().then((iframe) => {
      console.log('clickEvent has received the Promise and is now running more code');
    })
  }
}

const s = new SomeClass();
button.onclick = () => s.clickEvent();
<button id="button">click to call clickEvent</button>

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you're using Angular, I think you can convert the click event into an observable and then use the variety of operators such as exhaustMap to achieve this.

import { exhaustMap, fromEvent } from 'rxjs';
....
  @ViewChild('btnId') btnElementRef!: ElementRef<HTMLButtonElement>;

  ngAfterViewInit(): void {
    fromEvent(this.btnElementRef.nativeElement, 'click')
      .pipe(
        exhaustMap(() => this.createIframe())
      )
      .subscribe((iframe) => {
        // hard coded async logic here
      });
  }
);

This will ignore sub-sequent click until the Promise resolve first.

Further more, if you want to disable the button and display somekind of loading indicator, you can also add a variable to track that inside the stream using tap

  fromEvent(this.btnElementRef.nativeElement, 'click')
      .pipe(
        tap(() => isProcessing = true),
        exhaustMap(() => this.createIframe())
      )
      .subscribe((iframe) => {
        isProcessing = false;
        // hard coded async logic here
      });
about 4 years ago · Juan Pablo Isaza Denunciar
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