diff --git a/src/workers/dispatcher-main.ts b/src/workers/dispatcher-main.ts index f0ab67c..a290496 100644 --- a/src/workers/dispatcher-main.ts +++ b/src/workers/dispatcher-main.ts @@ -44,47 +44,40 @@ const dbReady = () => { return watchDbStatus()?.opened; }; -const dispatch = ( +const dispatch = async ( payload: any, callBack?: (error: any, result: any, id?: number | undefined) => void, live?: boolean ) => { - console.log({ caller: 'dispatcher-main / dispatch', payload }); + console.log({ caller: 'dispatcher-main / dispatch', payload, callBack }); if (worker === undefined) { init(); } - let returnValue; - until(dbReady, 100).then(() => { - // Wait until databases have been created by health.ts - if (callBack === undefined) { - /** If a callback function is not provided, return a promise */ - returnValue = new Promise((resolve, reject) => { - dispatch(payload, (error, result) => { - if (error) { - reject(error); - } else { - resolve(result); - } - }); + await until(dbReady, 100); // Wait until databases have been created by health.ts + if (callBack === undefined) { + /** If a callback function is not provided, return a promise */ + return new Promise((resolve, reject) => { + dispatch(payload, (error, result) => { + if (error) { + reject(error); + } else { + resolve(result); + } }); - } else { - /** Otherwise, use the callback function */ - dispatcherQueue.queue.set(dispatcherQueue.index, { callBack, live }); - const message = { - id: dispatcherQueue.index++, - payload: payload, - }; - worker.postMessage(message); - console.log({ - caller: 'dispatcher-main / message sent', - message, - dispatcherQueue, - }); - } - }); - if (returnValue) { - return returnValue; + }); } + /** Otherwise, use the callback function */ + dispatcherQueue.queue.set(dispatcherQueue.index, { callBack, live }); + const message = { + id: dispatcherQueue.index++, + payload: payload, + }; + worker.postMessage(message); + console.log({ + caller: 'dispatcher-main / message sent', + message, + dispatcherQueue, + }); }; export const cancelDispatch = (id: number) => {