replied on April 16, 2023
A technical lead who used to work for Laserfiche showed me this method which I find works the best in my opinion.
I have a alert message that needs to display if there are no courses for an instructor to choose from after lookups complete.

// Display the alert message when no courses are available to choose.
// The instructor id field will be blank if there are no courses
function ShowMessagesOnComplete() {
var elm = $('.instructor-id input').val();
if (!elm) {
$('.error-message').show();
}
}
// Hide the alert message initially
function HideMessagesOnStart() {
$('.error-message').hide();
}
//Create promise wrappers for page load events
function WaitForDocumentReady() {
const dfd = $.Deferred();
$(document).ready(() => {
console.log('[DOM Ready] Fired');
dfd.resolve();
});
return dfd.promise();
}
// Creates a promise that waits for the lookup handlers to be in place.
// It does this by listening for the onloadlookupfinished event and resolving
// the promise when it is fired.
function WaitForOnLoadLookupFinished() {
const dfd = $.Deferred();
const onLookupFinishedHandler = () => {
console.log('[On Load Lookup Finished] Fired');
// Show the alert if no courses are available after lookup finished
ShowMessagesOnComplete();
dfd.resolve();
};
const interuptHandler = () => {
console.log('[Cancel On Load Lookup] Fired');
dfd.resolve();
};
$(document).one('onloadlookupfinished', onLookupFinishedHandler);
$(document).one('cancelonloadlookupwait', interuptHandler);
return dfd.promise();
}
// Initialize promises by calling the $.when() method, which waits for all promises
// to be resolved before executing the callback function.
$.when(
WaitForOnLoadLookupFinished(),
WaitForDocumentReady().then(() => {
HideMessagesOnStart();
}));