JavaScript is asynchronous. The call to
$myDialog.dialog('open');
will not block, but rather will return immediately.
Instead, one must use event callbacks that you provide in the buttons
option of dialog.
This near-duplicate provides a good example:
jquery ui dialog box need to return value, when user presses button, but not working