Unable to use promise disposer pattern on SQLTransaction

  1. SDK Version: 40
  2. Platforms(Android/iOS/web/all): Android

I am trying to use the promise disposer pattern to prevent callback hell in my SQLite code. I coded it up already but it does not work, because it seems to schedule it to run immediately (as noted by the immedate(...) code in executeSQL.

I am asking in StackOverflow as well javascript - Does expo SQL lite allow for the promise disposer pattern with the transactions? - Stack Overflow

Here’s the codeblock I had

import type * as SQLite from 'expo-sqlite';

export class AsyncSQLTransaction implements SQLite.SQLTransaction {
  constructor(private tx: SQLite.SQLTransaction, private log = false) {}
  /**
   * fowards to the existing method
   * @param sqlStatement
   * @param args
   * @param callback
   * @param errorCallback
   */
  executeSql(
    sqlStatement: string,
    args?: any[],
    callback?: SQLite.SQLStatementCallback,
    errorCallback?: SQLite.SQLStatementErrorCallback
  ): void {
    this.tx.executeSql(sqlStatement, args, callback, errorCallback);
  }
  /**
   * Executes an SQL statement as a promise
   * @param sqlStatement
   * @param args arguments
   * @returns result set promise
   */
  q(sqlStatement: string, ...args: any): Promise<SQLite.SQLResultSet> {
    return new Promise((resolve, reject) => {
      if (this.log) {
        console.log(`> ${sqlStatement} ${JSON.stringify(args)}`);
      }
      this.tx.executeSql(
        sqlStatement,
        args,
        (tx, resultSet) => {
          this.tx = tx;
          resolve(resultSet);
          if (this.log) {
            console.log(`<`, JSON.stringify(resultSet, null, 2));
          }
        },
        (error) => {
          if (this.log) {
            console.log(`! ${sqlStatement} ${JSON.stringify(args)}`, error);
          }
          reject(error);
          return true;
        }
      );
    });
  }
}

But when I try to use it, in a db.transaction it only executes the first one but not the others. It appears when the promise gets handled it sets the transaction to complete and skips the rest of it.

The callback approach appears to work correctly, but it yields into several levels of nesting making the code hard to read.

I opened it up as a bug and snack as it appears to be one, but it is very fundamental to the core of expo-sqlite that I don’t think it will get fixed anytime soon.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.