title: Wait for completion handler to finish - Swift
body: <p>I am trying to check if UserNotifications are enabled and if not I want to throw an alert. So I have a function <code>checkAvailability</code> which checks multiple things, including the UserNotification authorization status.</p>
  
  <pre><code>func checkAvailabilty() -&gt; Bool {
  
      // 
      // other checking
      //
  
      var isNotificationsEnabled = false
      UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound], completionHandler: { (granted, error) in
  
                      if granted {
                          isNotificationsEnabled = true
                      }
                      else {
                          isNotificationsEnabled = false
                      }
                  })
              }
  
  
      if isNotificationsEnabled {
          return true
      }
      else {
          // Throw alert: Remind user to activate notifications
          return false
      }
  }
  </code></pre>
  
  <p>But the completion handler gets called too late. The function already returned <code>false</code> and after that the code in the colsure executes.</p>
  
  <p>I tried to put the whole statement <code>UNUserNotificationCenter.current().requestAuthorization()</code> in a synchronous dispatch queue but this didn't work.</p>
  
  <p>Another approach would be to return from inside the closure but I have no idea how to accomplish that.</p>