# Chapter 4: Why the need to double up on await for \`.notifications(for:)\`?

**URL:** <https://forums.kodeco.com/t/chapter-4-why-the-need-to-double-up-on-await-for-notifications-for/182371>\
**Category:** Modern Concurrency in Swift\
**Created:** [January 4, 2023, 2:21pm UTC](https://forums.kodeco.com/t/chapter-4-why-the-need-to-double-up-on-await-for-notifications-for/182371 "2023-01-04T14:21:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mayo](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mayo/32/110330_2.png) [@mayo](https://forums.kodeco.com/u/mayo)\
**Post date:** [January 4, 2023, 2:22pm UTC](https://forums.kodeco.com/t/chapter-4-why-the-need-to-double-up-on-await-for-notifications-for/182371/1 "2023-01-04T14:22:00Z")

</div>

In the body of `observeAppStatus` we end up with:

```auto
    for await _ in await NotificationCenter.default.notifications(for: UIApplication.willResignActiveNotification) {
      try? await say("\(username) went away", isSystemMessage: true)
    }

```

You can see there are two `awaits` – seemingly the first for awaiting the elements in the `AsyncStream`, and the second for awaiting the result of the `notifications(for:)` function call. The thing is that `notifications(for:)` is not marked `async`, but the compiler still requires the second `await` to compile. This is not the case if we use a nearly-identical example I’ll call `stupidGarbage`:

```auto
extension NotificationCenter {
  // Method from book, not marked async
  func notifications(for name: Notification.Name) -> AsyncStream<Notification> {
    return AsyncStream<Notification> { continuation in
      NotificationCenter.default.addObserver(forName: name, object: nil, queue: nil) { note in
        continuation.yield(note)
      }
    }
  }
  
  // Nearly identical method, also not marked async
  func stupidGarbage() -> AsyncStream<String> {
    return AsyncStream<String> { continuation in
      continuation.finish()
    }
  }
}

```

In this case, awaiting the elements doesn’t require the second await:

```auto
func observeAppStatus() async {
    // Removing the second await causes a compilation error
    for await _ in await NotificationCenter.default.notifications(for: UIApplication.willResignActiveNotification) {
      try? await say("\(username) went away", isSystemMessage: true)
    }
    
    // One await works fine here though
    for await thing in NotificationCenter.default.stupidGarbage() {
      try? await say(thing)
    }
  }

```

Is there some implicit thing here that I’m missing? Maybe a compiler error?

---

<div class="post-metadata">

**Author:** ![ralucat](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ralucat/32/110937_2.png) [@ralucat](https://forums.kodeco.com/u/ralucat)\
**Post date:** [February 23, 2023, 5:12pm UTC](https://forums.kodeco.com/t/chapter-4-why-the-need-to-double-up-on-await-for-notifications-for/182371/2 "2023-02-23T17:12:15Z")

</div>

The `notifications(for:)` await is due to the fact that `UIApplication` is annotated with `@MainActor`, so accessing the `UIApplication.willResignActiveNotification` notification name needs to cross the actor isolation layer, making it an async operation.

The code can be rewritten as:

```auto
    Task {
      for await _ in NotificationCenter.default.notifications(for: await UIApplication.willResignActiveNotification) {
        try? await say("\(username) went away", isSystemMessage: true)
      }
    }

```

If the notification name is obtained in a sync fashion, the second await is indeed not needed:

```auto
      for await _ in NotificationCenter.default.notifications(for: Notification.Name("Test")) {
        try? await say("\(username) went away", isSystemMessage: true)
      }

```

---

<div class="post-metadata">

**Author:** ![kpere](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/kpere/32/109586_2.png) [@kpere](https://forums.kodeco.com/u/kpere)\
**Post date:** [April 17, 2023, 4:15pm UTC](https://forums.kodeco.com/t/chapter-4-why-the-need-to-double-up-on-await-for-notifications-for/182371/3 "2023-04-17T16:15:43Z")

</div>

Thanks for the insight and adding points to explaining the code @ralucat. Great job

---

<div class="post-metadata">

**Author:** ![kpere](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/kpere/32/109586_2.png) [@kpere](https://forums.kodeco.com/u/kpere)\
**Post date:** [April 17, 2023, 4:15pm UTC](https://forums.kodeco.com/t/chapter-4-why-the-need-to-double-up-on-await-for-notifications-for/182371/4 "2023-04-17T16:15:53Z")

</div>

Closed on April 17, 2023, 4:15pm UTC


