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

218
Visualizações
Call async function in a while loop

I have to read a value from a BLE devive and then write to it another value so this is my function to do it:

   getBit = (device) => {
        try {
            device.readCharacteristicForService(this.state.sUuid, this.state.cUuid2)
            .then((read) => {
                switch (this.toHexByte(read.value)) {
                    case 'ee':
                        this.setState({ countC0: 0 });
                        break;
                    case 'c0':
                        this.setState({ countC0: this.state.countC0 + 1 });
                        break;
                    default: // the bit I want to stock
                        this.setState({
                            resId: [...this.state.resId, this.toHexByte(read.value)],
                            countC0: 0
                        });
                        break;
                }
                device.writeCharacteristicWithoutResponseForService(this.state.sUuid, this.state.cUuid2, this.state.respBit)
            })
            return
        }
        catch (err) {
            console.log("find catch error:", err);
        }
    }

but I have to call this function until I get the same bit ('c0') 3 time so I loop for it like that:

   while (countC0 < 15) {
     this.getBit(device);
     countC0++;
   }

The problem is that I receive Please report: Excessive number of pending callbacks: 501 error because I'm calling an async function in a while loop.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

this should do it

 getBit = async (device) => { try { const read = await device.readCharacteristicForService(this.state.sUuid, this.state.cUuid2) switch (this.toHexByte(read.value)) { case 'ee': this.setState({ countC0: 0 }); break; case 'c0': this.setState({ countC0: this.state.countC0 + 1 }); break; default: // the bit I want to stock this.setState({ resId: [...this.state.resId, this.toHexByte(read.value)], countC0: 0 }); } device.writeCharacteristicWithoutResponseForService(this.state.sUuid, this.state.cUuid2, this.state.respBit) } catch (err) { console.log("find catch error:", err); } }
 while (countC0 < 15) { await this.getBit(device); countC0++; }
about 4 years ago · Santiago Trujillo 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