Home > front end >  Why does this method call have to be marked with await even though it's not an async method?
Why does this method call have to be marked with await even though it's not an async method?

Time:11-05

If I needed to listen for a notification, I've always done like so:

NotificationCenter.default.addObserver(self, selector: #selector(foo), name: UIApplication.didEnterBackgroundNotification, object: nil)

But if I move that line within the body of an async method, the compiler suddenly wants that line to be marked with await:

func setup() async {
    let importantStuff = await someTask()
    // store my important stuff, now setup is complete

    // This line won't compile without the await keyword.
    // But addObserver is not even an async method            
  • Related