How do I wait for the modal selected value?

How do I wait for the modal selected value?

I trigger to show the modal when user click on some element and ask them to make a choice.
Currently is to choose one color out of two.
And then I will use the color selected to create some element and add to the document.

I tried to use global variable to store the choice but that is too slow for my element creation.
Is there a synchronize way to wait for the modal to return value?

The following is invoked when user click on a specific element

  1. var SelectedColor = '#0000FF';  //global variable
  2. function CreateRoute(marker) {
  3.                 //create route
  4.                 var vertex = [];
  5.                 vertex.push(marker.getLatLng());
  6.                 //Block here and ask for color
  7.                 SelectedColor = null;
  8.                 $("#ChooseColor").dialog('open');
  9.                 //this one should wait callback
  10.                 var polyline = createGPolyline(vertex, SelectedColor, 5, 0.5);

  11.                 $gmap.addOverlay(polyline);
  12.                 polyline.enableDrawing({});
  13.                 GEvent.addListener(polyline, "endline", function() { BindEditRoute(polyline) });
  14.                 GEvent.addListener(polyline, "lineupdated", function() { UpdateRoute(polyline) });
  15.             }
What should I do to work around this?