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

130
Visualizações
Converting call back to async/await functions

I'm trying to concert a callback function to stand chained functions, but could not manage it,

the callback function is:

db.transaction(
 function(t){
  t.executeSql("SELECT * FROM cal_list where calories > ?" ,[min_cal_amount], function(t,r){
     for (var i=0; i < r.rows.length; i++){
      food = r.rows.item(i).food;
      amount_of_calories = r.rows.item(i).amount_of_calories;
      serving_size = r.rows.item(i).serving_size;           
        
      list.innerHTML +="<li>"+food+" has "+amount_of_calories+" KCAL worth of calories.</li>";
                    
        }
    }, 
            
    function(t,e){alert(e.message);})
}
);

How can I convert to to async..await or try..then..catch?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If your database library exposes db.transaction and t.executeSQL in a promise based api, it would look roughly like this:

async function getResults() {

    const transaction = await db.transaction();

    try {
        const result = await transaction.executSql(
            'SELECT * FROM cal_list where calories > ?',
            [min_cal_amount]
        );

        for (var i = 0; i < result.rows.length; i++) {
            // work with result data
        }
    }
    catch (e) {
        // rollback transaction, handle error as appropriate
    }
}

If your library does not expose those functions in a promise based api, you can use util.promisify to wrap those functions that expect a callback, and then use the above snippet.

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