title: Swift closures - order of code execution in regards to value assignment
body: <p>I would like to know in details the proper order of execution when working on data in a completion handler. I have the following function definition on my Event class:</p>
  
  <pre><code>@nonobjc public class func fetchEvents(completion: @escaping ([Event]) -&gt; () = { _ in }) -&gt; [Event] {
      let fetch = self.fetchEventRequest()
  
      var entities: [Event] = []
      context.perform {
          entities = try! fetch.execute()
          completion(entities)
      }
      return entities
  }
  </code></pre>
  
  <p>And I call the above function from my view controller as such:</p>
  
  <pre><code>events = MyEvents.fetchEvents() { entities in
          if (entities.count == 0) {
              self.events = Event.getEventsFromAPI()
          } 
          print(events.count) - // returns 0 at this point
          self.eventsTableView.reloadData()
      }
  </code></pre>
  
  <p>Upon calling <code>self.eventsTableView.reloadData()</code> my <code>events</code> variable is still empty. I would assume that since the completion handler executes after successful execution of the code inside the function, the <code>events</code> var would have already been assigned the returned value, no?</p>