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

164
Visualizações
Javascript await sequence problem in expo-sqlite

I am using react native expo to do the init data load and insert it into DB. But seems the sequence is not my expectations.

and below is my init function

import { getData } from './GetData'
const db = SQLite.openDatabase('db.db') // returns Database object

const initDB = async (res: any) => {
    await db.transaction(async tx => {
        await tx.executeSql(`create table if not exists table (
            id integer primary key AUTOINCREMENT not null,
            ...... // create db query
        )`, [], 
        () => {
            console.log("created table")
        })
        
        await tx.executeSql(insertQuery
            , res.list,
            () => {
                console.log(`inserted data success`)
            },
            (_, err): boolean | any => {
                console.log(`err: ${err}`)
            }
        )
    })
}

export const initLoading = async () => {
    const res = await getData() // get data from API
    await initDB(res)
}
await new Promise(async resolve => {
    await initLoading()
    console.log("init done")
})

my expected console log should be

created table
inserted data success
init done

but the result is

init done
created table
inserted data success

what thing I am doing wrong to make the sequence incorrect?

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

0

The problem is that in db.transaction you pass a function as argument. You dont await the function you passed, but you await just the execution of db.transaction. I think the best way to fix this is to wrap db.transaction in a Promise and resolve it when you need it to.

const initDB = async (res: any) => {
    return new Promise(async resolve => 
        await db.transaction(async tx => {
            await tx.executeSql(`create table if not exists table (
                id integer primary key AUTOINCREMENT not null,
                ...... // create db query
            )`, [], 
            () => {
                console.log("created table")
            })
            
            await tx.executeSql(insertQuery
                , res.list,
                () => {
                    console.log(`inserted data success`)
                    // Resovle when the data is successful
                    resolve()
                },
                (_, err): boolean | any => {
                    console.log(`err: ${err}`)
                }
            )
        })
    )
}

I would recommend you to also resolve on error. Else your code get's stuck.

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