# Needed: suspend fun PublisherScope.awaitDemand()

**URL:** <https://discuss.kotlinlang.org/t/needed-suspend-fun-publisherscope-awaitdemand/9476>\
**Category:** Libraries\
**Created:** [September 17, 2018, 4:58pm UTC](https://discuss.kotlinlang.org/t/needed-suspend-fun-publisherscope-awaitdemand/9476 "2018-09-17T16:58:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![baltiyskiy](https://avatars.discourse-cdn.com/v4/letter/b/85e7bf/32.png) [@baltiyskiy](https://discuss.kotlinlang.org/u/baltiyskiy)\
**Post date:** [September 17, 2018, 4:58pm UTC](https://discuss.kotlinlang.org/t/needed-suspend-fun-publisherscope-awaitdemand/9476/1 "2018-09-17T16:58:20Z")

</div>

This quesiton is about `kotlinx-coroutines-reactive`.

The idea with using suspend functions for backpressure is nice, but the current design has a significant shortcoming: a Kotlin-style publisher _must_ have the next element calculated, and only then can it learn that it’s not needed (via `send()`). This is a problem for “expensive” publishers: those for which it’s costly to calculate elements, and you only want to do it if there’s demand from upstream.

Example use case: a `Publisher` backed by polling a DB table. In this case, we need to stop polling while there is no demand from upstream. However, the current API doesn’t provide a good way of doing this; the best you can do is to replace DB polling with polling `PublisherScope.isFull`.

I’d suggest to add `PublisherScope.awaitDemand()` method that would suspend until there’s demand from upstream.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [September 17, 2018, 5:38pm UTC](https://discuss.kotlinlang.org/t/needed-suspend-fun-publisherscope-awaitdemand/9476/2 "2018-09-17T17:38:24Z")

</div>

I think [this issue](https://github.com/Kotlin/kotlinx.coroutines/issues/254) is what you are looking for.

---

<div class="post-metadata">

**Author:** ![baltiyskiy](https://avatars.discourse-cdn.com/v4/letter/b/85e7bf/32.png) [@baltiyskiy](https://discuss.kotlinlang.org/u/baltiyskiy)\
**Post date:** [September 18, 2018, 10:21am UTC](https://discuss.kotlinlang.org/t/needed-suspend-fun-publisherscope-awaitdemand/9476/3 "2018-09-18T10:21:42Z")

</div>

That discussion touches a lot of things, but none of them are about `PublisherScope`. I do need a reactive stream to interact with non-Kotlin code compliant to reactive stream spec. So even though a generic Kotlin-based solution is great, I don’t immediately see how it would help here. Thanks for the link though, I like the direction they’re taking.
