@@ -35,8 +35,8 @@ export function waSqlitePool(path: string): SqliteDriverConnectionPool {
3535const m = new mutex . Mutex ( ) ;
3636
3737class StatementImpl implements SqliteDriverStatement {
38- private preparePromise : Promise < void > ;
39- private bindPromise ?: Promise < void > ;
38+ private preparePromise : Promise < { error : SqliteError | null } > ;
39+ private bindPromise ?: Promise < { error : SqliteError | null } > ;
4040 private columns : string [ ] = [ ] ;
4141
4242 private stringRef ?: number ;
@@ -52,7 +52,7 @@ class StatementImpl implements SqliteDriverStatement {
5252 }
5353
5454 async prepare ( ) {
55- await m . runExclusive ( ( ) => this . _prepare ( ) ) ;
55+ return await m . runExclusive ( ( ) => this . _prepare ( ) ) ;
5656 }
5757
5858 async _prepare ( ) {
@@ -66,22 +66,36 @@ class StatementImpl implements SqliteDriverStatement {
6666
6767 this . statementRef = r ?. stmt ;
6868 this . columns = sqlite3 . column_names ( this . statementRef ! ) ;
69+ return { error : null } ;
6970 } catch ( e : any ) {
70- throw new SqliteError ( {
71- code : 'SQLITE_ERROR' ,
72- message : e . message
73- } ) ;
71+ return {
72+ error : new SqliteError ( {
73+ code : 'SQLITE_ERROR' ,
74+ message : e . message
75+ } )
76+ } ;
77+ }
78+ }
79+
80+ private async _waitForPrepare ( ) {
81+ const { error } = await ( this . bindPromise ?? this . preparePromise ) ;
82+ if ( error ) {
83+ throw error ;
7484 }
7585 }
7686
7787 async getColumns ( ) : Promise < string [ ] > {
78- await this . preparePromise ;
88+ await this . _waitForPrepare ( ) ;
7989 return sqlite3 . column_names ( this . statementRef ! ) ;
8090 }
8191
8292 bind ( parameters : SqliteParameterBinding ) : void {
83- this . bindPromise = this . preparePromise . then ( async ( ) => {
93+ this . bindPromise = this . preparePromise . then ( async ( result ) => {
94+ if ( result . error ) {
95+ return result ;
96+ }
8497 await m . runExclusive ( ( ) => this . bindImpl ( parameters ) ) ;
98+ return { error : null } ;
8599 } ) ;
86100 }
87101
@@ -125,13 +139,12 @@ class StatementImpl implements SqliteDriverStatement {
125139 }
126140
127141 async step ( n ?: number , options ?: StepOptions ) : Promise < SqliteStepResult > {
128- await this . preparePromise ;
142+ await this . _waitForPrepare ( ) ;
143+
129144 return await m . runExclusive ( ( ) => this . _step ( n , options ) ) ;
130145 }
131146
132147 async _step ( n ?: number , options ?: StepOptions ) : Promise < SqliteStepResult > {
133- await this . preparePromise ;
134-
135148 try {
136149 if ( this . done ) {
137150 return { done : true } ;
@@ -185,7 +198,10 @@ class StatementImpl implements SqliteDriverStatement {
185198 }
186199
187200 async _finalize ( ) {
201+ // Wait for these to complete, but ignore any errors.
202+ // TODO: also wait for run/step to complete
188203 await this . preparePromise ;
204+ await this . bindPromise ;
189205
190206 if ( this . statementRef ) {
191207 sqlite3 . finalize ( this . statementRef ) ;
0 commit comments