is it possible to get coroutine scope object insid...
# coroutines
d
is it possible to get coroutine scope object inside of coroutine? for example:
Copy code
private suspend fun <E> ReceiveChannel<E>.throttle(): ReceiveChannel<E> {
    // want to launch {} in current scope
}
w
I think you would provide scope as a parameter to
throttle
, then use that scope to
launch
d
hmm. not very convenient. no other ways?
s
coroutineScope(coroutineContext) { } ?
although I recall they dont recommend this way
d
the signature doesn't allow this
Copy code
public suspend fun <R> coroutineScope(block: suspend CoroutineScope.() -> R): R
and i would like to run the
launch
in worker thread
i mean return result immediately
s
one sec
d
do you mean?
Copy code
val scope = CoroutineScope(Dispatchers.Default)
scope.launch(Dispatchers.Default) {...}
s
Copy code
CoroutineScope(coroutineContext).run {
                    launch {
                        
                    }
                    
                }`
d
looks legit. thanks
I recall they dont recommend this way
do you know why?
s
I guess because you're technically creating a new CoroutineScope it just has all the elements of the one of the caller. There's another way too. Not sure though
ah nevermind you're already using ReceiveCHannel as the receiver
g
not very convenient
This is the most explicit and safe way, because caller knows exactly on which scope new coroutine will be executed, now instead you create new scope that uses job of parent But I would say that for this particular use case it's probably fine to make this channel`s operator signature clear