Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

149
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda