| 36 | } |
| 37 | |
| 38 | enqueue(asyncFunction: (...args: any[]) => Promise<unknown>) { |
| 39 | // This outer promise might seems superflous since down below we return asyncFunction().then(resolve, reject). |
| 40 | // However, this ensures that this.previous will never be a rejected promise so the queue will |
| 41 | // always keep going, while still communicating rejection from asyncFunction to the user. |
| 42 | return new Promise((resolve, reject) => { |
| 43 | this.previous = this.previous.then(() => { |
| 44 | this.rejectCurrent = reject; |
| 45 | if (this.closed) { |
| 46 | return reject( |
| 47 | new ConnectionError( |
| 48 | new AsyncQueueError( |
| 49 | 'the connection was closed before this query could be executed' |
| 50 | ) |
| 51 | ) |
| 52 | ); |
| 53 | } |
| 54 | return asyncFunction().then(resolve, reject); |
| 55 | }); |
| 56 | }); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | export default AsyncQueue; |