How does 'dispatch' work? (Test example provided)



I ask some guidance in understanding how the dispatch() instruction works.
API Descirption

The code example below is coded into
( System: Atom V1.21.1 on Windows7. )

# ===  ===
waitTask = false  

taskDone = (event) ->
  # make sure correct trigger
  return false unless waitTask
  return false unless event.type is 'core:save-as' 

  waitTask = false
  console.log "completed"

atom.commands.add 'atom-text-editor', 'test command', ->
  return false unless _editor = atom.workspace.getActiveTextEditor()
  # return false unless _editor.getGrammar().scopeName is 'text.plain'
  # @1
  console.log "prepared"
  waitTask = true
  atom.commands.onDidDispatch(taskDone)  # @2
  atom.commands.dispatch(atom.views.getView(_editor), 'core:save-as') # @3
  console.log "started"
  return true

When the command is triggered the following occurs:

  1. The save-as dialog pops up as if the code is holding at line @1.
  2. After closing the dialog, all three text lines come up at once on the developer console:

When swapping lines @2 and @3, the completed text line come up only when the save-as dialog is closed by cancel.

Questions and comments

  1. Is the onDidDispatch() required? Or is the code “frozen” until the dispatch command is complete?

  2. Why is the lines before dispatch() not executed? Is there a rule that applies?

  3. Why if lines @2 and @3 are swapped does taskDone() execute only when dialog cancel is used? Is the save-as command dispatched twice?

  4. More thoughts / comment are welcome.

Thank you for your attention.
- Dan