Javascript expo-sqlite 中的 await 序列问题
Javascript await sequence problem in expo-sqlite
我正在使用 React Native Expo 进行初始数据加载并将其插入数据库。
不过好像顺序不是我的预期。
下面是我的初始化函数
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")
})
我预期的控制台日志应该是
created table
inserted data success
init done
但结果是
init done
created table
inserted data success
我做错了什么导致顺序不正确?
问题是在 db.transaction
中你传递了一个函数作为参数。您不等待传递的函数,而只是等待 db.transaction
的执行。我认为解决此问题的最佳方法是将 db.transaction
包装在 Promise 中并在需要时解决它。
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}`)
}
)
})
)
}
我建议您也解决错误。否则你的代码会卡住。
我正在使用 React Native Expo 进行初始数据加载并将其插入数据库。 不过好像顺序不是我的预期。
下面是我的初始化函数
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")
})
我预期的控制台日志应该是
created table
inserted data success
init done
但结果是
init done
created table
inserted data success
我做错了什么导致顺序不正确?
问题是在 db.transaction
中你传递了一个函数作为参数。您不等待传递的函数,而只是等待 db.transaction
的执行。我认为解决此问题的最佳方法是将 db.transaction
包装在 Promise 中并在需要时解决它。
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}`)
}
)
})
)
}
我建议您也解决错误。否则你的代码会卡住。