Bug fix (bad promise magic!)

This commit is contained in:
Eric van der Vlist 2023-02-11 18:21:25 +01:00
parent 853b158dcf
commit 51250c1880
1 changed files with 25 additions and 32 deletions

View File

@ -44,21 +44,19 @@ const dbReady = () => {
return watchDbStatus()?.opened; return watchDbStatus()?.opened;
}; };
const dispatch = ( const dispatch = async (
payload: any, payload: any,
callBack?: (error: any, result: any, id?: number | undefined) => void, callBack?: (error: any, result: any, id?: number | undefined) => void,
live?: boolean live?: boolean
) => { ) => {
console.log({ caller: 'dispatcher-main / dispatch', payload }); console.log({ caller: 'dispatcher-main / dispatch', payload, callBack });
if (worker === undefined) { if (worker === undefined) {
init(); init();
} }
let returnValue; await until(dbReady, 100); // Wait until databases have been created by health.ts
until(dbReady, 100).then(() => {
// Wait until databases have been created by health.ts
if (callBack === undefined) { if (callBack === undefined) {
/** If a callback function is not provided, return a promise */ /** If a callback function is not provided, return a promise */
returnValue = new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
dispatch(payload, (error, result) => { dispatch(payload, (error, result) => {
if (error) { if (error) {
reject(error); reject(error);
@ -67,7 +65,7 @@ const dispatch = (
} }
}); });
}); });
} else { }
/** Otherwise, use the callback function */ /** Otherwise, use the callback function */
dispatcherQueue.queue.set(dispatcherQueue.index, { callBack, live }); dispatcherQueue.queue.set(dispatcherQueue.index, { callBack, live });
const message = { const message = {
@ -80,11 +78,6 @@ const dispatch = (
message, message,
dispatcherQueue, dispatcherQueue,
}); });
}
});
if (returnValue) {
return returnValue;
}
}; };
export const cancelDispatch = (id: number) => { export const cancelDispatch = (id: number) => {