# Whence comes the context for new coroutines?

**URL:** <https://discuss.kotlinlang.org/t/whence-comes-the-context-for-new-coroutines/15234>\
**Category:** Libraries\
**Created:** [November 30, 2019, 3:47am UTC](https://discuss.kotlinlang.org/t/whence-comes-the-context-for-new-coroutines/15234 "2019-11-30T03:47:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mtimmerm](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mtimmerm/32/7160_2.png) [@mtimmerm](https://discuss.kotlinlang.org/u/mtimmerm)\
**Post date:** [November 30, 2019, 3:47am UTC](https://discuss.kotlinlang.org/t/whence-comes-the-context-for-new-coroutines/15234/1 "2019-11-30T03:47:12Z")

</div>

When you call `block.startCoroutine(completion)` or `block.createCoroutineUnintercepted(completion)`, is the context for the new coroutine (and the `Continuation` returned by `createCoroutineUnintercepted`) copied from `completion.context`?

That should really be in the docs, but it isn’t.

---

<div class="post-metadata">

**Author:** ![nickallendev](https://avatars.discourse-cdn.com/v4/letter/n/cdc98d/32.png) [@nickallendev](https://discuss.kotlinlang.org/u/nickallendev)\
**Post date:** [December 2, 2019, 6:34pm UTC](https://discuss.kotlinlang.org/t/whence-comes-the-context-for-new-coroutines/15234/2 "2019-12-02T18:34:58Z")

</div>

Poking around the source code shows that the answer is yes, as expected (where else would it come from?).

Why are you investigating these methods? The expected way to create a coroutine is by calling `launch` or `async`.

---

<div class="post-metadata">

**Author:** ![mtimmerm](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mtimmerm/32/7160_2.png) [@mtimmerm](https://discuss.kotlinlang.org/u/mtimmerm)\
**Post date:** [December 3, 2019, 2:36am UTC](https://discuss.kotlinlang.org/t/whence-comes-the-context-for-new-coroutines/15234/3 "2019-12-03T02:36:31Z")

</div>

Thanks for looking that up. Where did you find it? There isn’t any other obvious place where it would come from, but I didn’t think that getting it from the completion was really obvious either. At the time I didn’t otherwise care what context was used to resume the completion, so I used `EmptyCoroutineContext`

I’m using these methods in the implementation of this super-fun function, which is part of an Input/OutputStream replacement library:

```
/**
 * Create a coroutine that is executed incrementally as the returned
 * iterator is advanced.
 *
 * Until the end of the iterated sequence, the coroutine only executes
 * during calls to [Iterator.next].
 *
 * Each call to [Iterator.next] returns a suspending function that
 * should be called before [Iterator.next] is called again. These functions
 * are produced out to the caller whenever the coroutine suspends, and
 * calling the produced function will delay until the coroutine is ready to
 * proceed.
 *
 * Suspending functions can also be produced out to the caller via
 * [IncrementalScope.yield] on the block's receiver.
 *
 * If the caller fails to call a produced suspending function when required,
 * or fails to interact with other side-effects of the coroutine in any
 * required way, then the coroutine should terminate as soon as it regains
 * control.
 *
 * When the coroutine terminates, or when it is cancelled via cancellation
 * of the [Job] in the parent [CoroutineScope], any remaining (clean up) work
 * will be launched in the parent scope, and subsequent calls to [Iterator.hasNext]
 * will return false.
 */
fun CoroutineScope.launchIncremental(block: suspend IncrementalScope.() -> Unit)
    :Iterator<suspend () -> Unit> {
    TODO()
}

```

---

<div class="post-metadata">

**Author:** ![nickallendev](https://avatars.discourse-cdn.com/v4/letter/n/cdc98d/32.png) [@nickallendev](https://discuss.kotlinlang.org/u/nickallendev)\
**Post date:** [December 3, 2019, 7:19am UTC](https://discuss.kotlinlang.org/t/whence-comes-the-context-for-new-coroutines/15234/4 "2019-12-03T07:19:39Z")

</div>

That iterator of suspend lambdas looks kinda unwieldy. Have you looked at [Flow](https://kotlinlang.org/docs/reference/coroutines/flow.html)?

I recommend sticking to the higher level methods described in the [Coroutines Guide](https://kotlinlang.org/docs/reference/coroutines/coroutines-guide.html).

---

<div class="post-metadata">

**Author:** ![mtimmerm](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mtimmerm/32/7160_2.png) [@mtimmerm](https://discuss.kotlinlang.org/u/mtimmerm)\
**Post date:** [December 3, 2019, 1:15pm UTC](https://discuss.kotlinlang.org/t/whence-comes-the-context-for-new-coroutines/15234/5 "2019-12-03T13:15:04Z")

</div>

It’s got a different purpose. It’s more like a way to remove the scope restrictions from `Sequence`s
