* Wrap an event function in another event that resolves a promise when done. * * @param event Event function to wrap. * @returns The wrapped event function and a promise that resolves when the event is done executing, * or rejects if it rejects or throws an error. * [ wrapped
(event: EventFunction)
| 88 | * [ wrapped event, promise ] |
| 89 | */ |
| 90 | function wrapEvent(event: EventFunction): [ () => Promise<void>, Promise<void> ] { |
| 91 | // Exposed promise callbacks |
| 92 | let resolvePromise: () => void; |
| 93 | let rejectPromise: (error: any) => void; |
| 94 | // Wrap the event in another event |
| 95 | const wrappedEvent = async () => { |
| 96 | try { |
| 97 | await executeEventFunction(event); |
| 98 | resolvePromise(); |
| 99 | } catch (error) { |
| 100 | rejectPromise(error); |
| 101 | } |
| 102 | }; |
| 103 | // Create the promise to return (and expose its callbacks) |
| 104 | const promise = new Promise<void>((resolve, reject) => { |
| 105 | resolvePromise = resolve; |
| 106 | rejectPromise = reject; |
| 107 | }); |
| 108 | // Return wrapped event and promise |
| 109 | return [wrappedEvent, promise]; |
| 110 | } |
| 111 | |
| 112 | function noop() { /* Do nothing. */ } |