These model methods should instead return a promise that wraps the db API calls, so that we can use async/await in any calling code (controllers), e.g.
class Program extends Base {
...
public static count(): Promise<Number> {
return new Promise((resolve, reject) => {
this.db.readTransaction((tx: SQLTransaction): void => tx.executeSql(
"SELECT COUNT(*) AS ProgramCount FROM Program", [],
(_: SQLTransaction, resultSet: SQLResultSet): void => resolve(resultSet.rows.item(0).ProgramCount,
(): boolean => reject(0)
);
});
}
}
// Somewhere else...
async someFunc() {
const count = await Program.count();
// do something with count
}
The
WebSQL
async API predates native ES2015 promises (or ES2017async
/await
) ; so it uses callbacks, e.g.The models extend the same callback-style, e.g.
These model methods should instead return a promise that wraps the
db
API calls, so that we can useasync
/await
in any calling code (controllers), e.g.