Run button handler asynchronously


#1

Hi, I have an alert controller which has some checkboxes, but the handler takes too long to finish.

What I want is to click the checkbox and that the handler is handled asynchronously, meaning that I don’t have to wait for it to finish. Adding async didn’t work.

My code for this is:

this.listaVideosAux.forEach(lista => {
      alert.addInput({
        type: 'checkbox',
        label: lista.nombre,
        value: lista.nombre,
        checked: true,
        handler: async checkBox => {
          if (checkBox.checked) {
            this.categoriasFiltro.push(checkBox.value)
          } else {
            this.categoriasFiltro = this.categoriasFiltro.filter(categoria => categoria != checkBox.value)
          }
          this.filtrar();
        }
      });
    });

Thanks


#2

What I want is that once you’ve pressed a checkbox it does the “pressed” effect. I don’t mind if it takes some time to do what the handler wants to do. But as it is now, it’ll do that effect once the handler is done which looks horrible to me.


#3

Assuming filtrar is the long-running task, that’s all you really need to make asynchronous. Once it is, it will return immediately, and your handler will be considered “done” as soon as filtrar has begun.


#4

Even if I used async without await, it was still waiting. What I’ve do is to remove the async modifier and return a promise. Then I’ve put what’s inside filtrar into a settimeout and out of that settimeout and at the end the method I resolve the promise.