I have a complex, lengthy asynchronous process written in TypeScript/JavaScript that spans multiple libraries and functions. Once the data processing is complete, it triggers a function processComplete()
to indicate its finish:
processComplete(); // Signaling the end of processing
The current implementation of the processComplete()
function is as follows:
let finished = false;
function processComplete() {
finished = true;
}
To check if the process is complete, other parts of the code either rely on timeouts or use process.nextTick
to continuously monitor the finished
variable in loops. This approach is convoluted and inefficient.
Instead, I would like to enable various async
functions to utilize the await
keyword to wait until the process is finished:
// This snippet will be scattered in different sections of the codebase
await /* some condition for completion */;
console.log("Process completed!");
If I were coding in C on Windows, I would employ an event synchronization primitive where the logic resembles this:
Event done;
void processComplete() {
SetEvent(done);
}
// Elsewhere, placed in numerous locations
WaitForSingleObject(done, INFINITE);
console.log("Process completed!");
In JavaScript or TypeScript, instead of toggling a boolean flag like finished
, what actions could processComplete
take to wake up multiple functions waiting using await
? Essentially, how can I implement event synchronization using await
, async
, or Promise
s?