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