https://kotlinlang.org logo
Join the conversationJoin Slack
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by Linen
coroutines
  • m

    marcinmoskala

    11/17/2017, 7:35 AM
    Or maybe I can help with making common module with expected blocks for coroutines features and platform-specific modules for them for js and jvm?
    c
    • 2
    • 3
  • d

    Dave Leeds

    11/18/2017, 3:32 AM
    Hey all - satisfying my curiosity here... is there any way to
    suspend val
    (say, with
    get() {}
    or a delegate) at this point? I tried a few things and nothing worked, but I also wasn't sure if I was just doing it wrong. 🙂
    e
    k
    • 3
    • 4
  • s

    Sorin

    11/19/2017, 1:45 PM
    I have started using
    compile 'org.jetbrains.kotlinx:kotlinx-coroutines-android:0.19.3'
    however there is some rightful so skepticism from some of my colleagues. I would like to know if it is safe to use coroutines in production. I need a professional statement fro my colleagues. I mostly use
    suspend
    keyword and
    launch()
    g
    • 2
    • 1
  • m

    marcinmoskala

    11/20/2017, 8:06 AM
    Anyone have already implemented delay in Kotlin/JS?
    g
    • 2
    • 4
  • k

    kirillrakhman

    11/20/2017, 8:52 AM
    I was wondering, what's the current way to create a coroutine on JS? there are no builders if I'm not mistaken. Should I write my own oder can I just get away with making my
    main
    function
    suspend
    ?
    k
    g
    • 3
    • 5
  • s

    satejs

    11/20/2017, 9:46 AM
    I just saw the talk by @elizarov. He mentions a second talk, is there a link to that?
    f
    u
    • 3
    • 4
  • g

    gildor

    11/20/2017, 10:43 AM
    in this case
    async()
    is function (coroutine builder) and
    await
    is a function in interface Deferred (returned from async()) and both of them are part of kotlinx.coroutines library, not a part of language or stdlibrary. You can write own implementations that doesn’t coupled with particular Promise/Feature implementation like in languages where async/await is part of the language
    s
    • 2
    • 3
  • m

    marcinmoskala

    11/20/2017, 10:50 AM
    https://github.com/MarcinMoskala/JSCoroutinesRecipes
    g
    • 2
    • 1
  • e

    elizarov

    11/20/2017, 12:40 PM
    No performance difference, but a big functional one. See 😒tackoverflow: https://stackoverflow.com/questions/46226518/what-is-the-difference-between-launch-join-and-async-await-in-kotlin-coroutines
    ✅ 5
    s
    • 2
    • 1
  • g

    gildor

    11/22/2017, 6:16 AM
    Maybe you will share this error message, otherwise it’s hard to help you
    s
    • 2
    • 4
  • d

    dave08

    11/22/2017, 2:34 PM
    @marcinmoskala
    runBlocking
    and
    join
    all jobs with
    delay
    after, have
    refreshList
    as mocked interface, and check if being called expected x times for delay?
    m
    • 2
    • 6
  • j

    jimn

    11/23/2017, 5:22 AM
    at this time intellij is not honoring launch{} keyword from inside main
    g
    • 2
    • 13
  • j

    jimn

    11/23/2017, 5:41 AM
    @jw either the syntax is wrong, the imports are or the pom. I'm just trying to find a working example to paste in and tweak it from there.
    g
    • 2
    • 5
  • m

    matej

    11/24/2017, 3:48 PM
    Is the warning for coroutines
    The feature "coroutines" is experimental
    something new? I didn't notice it until today, and now all my
    run
    and
    launch
    invocations are highlighted
    s
    l
    d
    • 4
    • 4
  • j

    jmfayard

    11/26/2017, 12:07 PM
    I've been thinking about why, while I've been interested about couroutines since kotlin 1.1 came out, I have been reluctant to actually use them in any of my projects yet. It's not because I'm concerned about the maturity of the libs. It's not because "they are still experimental", while I could use this lame excuse if asked out of the blue. It's not because of the documentation, the documents and talks from @elizarov are great It's that I remember when I first rushed to use Promises (in JS) then RxJava in my projects, I didn't have the practical experience to use them correctly and made a LOT of mistakes Coroutines do seem easier to get right, but asynchrononicity is hard no matter what. I have no practical experiences with the style of programming with channels and actors for example. So the real issue is that I lack a safe playground where I could practice all these new toys, hopefully not only with "delay()" but also simple UX, DB and network stuff. so the TLDR is that having and completing a kotlin #koans for coroutines would boost my confidence in my own ability to use them correctly!
    ➕ 6
    s
    • 2
    • 1
  • d

    dekans

    11/26/2017, 1:11 PM
    I can't immediatly switch to kotlin on my work project. So I started a side project last june with kotlin and coroutines. And it's been months of refactoring, following my comprehension of coroutines! So I +1, starting coroutines immediately in a professionnal project isn't wise.
    l
    • 2
    • 4
  • d

    dave08

    11/28/2017, 8:41 AM
    @elizarov How would you simplify that Ion integration? With cancel in consideration and that coroutine should be used instead of threads... Im not sure what parts of that implementation are necessary and what not? This is just an example, but ive seen other integrations that also do complex things for what I thought should have been simple. I wonder if they want to avoid an extra dependency, or people are still not grasping how to do the integration properly... maybe a set of guidlines would have been good in such a case...?
    g
    • 2
    • 12
  • c

    christophsturm

    11/28/2017, 8:51 AM
    what is the overhead of withTimeout? just one timer for the cancel delay?
    e
    • 2
    • 1
  • f

    fstn

    11/28/2017, 9:01 AM
    thanks @gildor
    g
    • 2
    • 1
  • c

    christophsturm

    11/28/2017, 3:58 PM
    this is probably not coroutine specific, but is it possible to detect blocking code? i have a withTimeout block, with a one second timeout, and sometimes it takes 5 seconds instead. I think that the lib that I'm using is reconnecting and that its blocking when it does that. but it would be very useful to get a stacktrace of the blocking method in that case
    l
    • 2
    • 1
  • c

    cy

    11/29/2017, 10:02 AM
    notice that you will have all 100 coroutines running in parallel, just not concurrently
    g
    • 2
    • 1
  • e

    elizarov

    11/29/2017, 10:20 AM
    You don’t need to explicitly do
    join
    if you rely on parent/child rels to split one large task into parallel subtasks. So, to do
    mySuspendingTask
    100 times in parallel you can just do:
    fun main(args: Array<String>) = runBlocking {
        (0..100).forEach { launch(coroutineContext) { mySuspendingTask() } }
    }
    Now you are explicit about parallelism — you’ll launched concurrent subtask. No worries, since they are very cheap. You can run millions. You outer job (in this case — main) automatically waits for all the children to complete.
    g
    • 2
    • 1
  • e

    elizarov

    11/29/2017, 10:22 AM
    Btw,
    .map { it.await() }
    seems to be a common pattern. We might want to introduce extension like
    .awaitAll()
    for it.
    👍 12
    g
    b
    j
    • 4
    • 29
  • p

    Paul Woitaschek

    11/29/2017, 10:49 AM
    In my case it's a network request, that's why I want to limit the amount of concurrent requests
    g
    • 2
    • 1
  • e

    elizarov

    11/29/2017, 10:55 AM
    val reqs: List<Request> = ... // list of original requests
    val chan = Channel<Request>()
    launch { for (req in reqs) chan.send(req) } // send all reqs to chan
    repeat(n) { // craete a pool of limited number workers
        launch { for (req in chan) processRequest(req) }
    }
    g
    • 2
    • 2
  • v

    voddan

    11/29/2017, 11:11 AM
    BTW, how do I limit the number of concurrent children my coroutine might have? The suggested way of using a limited thread pool can't be used as-is since I have to use the parent context to have the parent-child relationship.
    fun main(args: Array<String>) = runBlocking {
        (0..100).forEach { 
             launch(coroutineContext) {   // <- limit the number of concurrent threads ?
                 mySuspendingTask() 
             } 
        }
    }
    e
    • 2
    • 1
  • e

    elizarov

    11/29/2017, 1:42 PM
    Limited parallelism seems to be quite a common pattern, so here is a bunch of ideas on what we can improve: https://github.com/Kotlin/kotlinx.coroutines/issues/172
    👍 2
    a
    • 2
    • 3
  • l

    louiscad

    11/30/2017, 9:39 AM
    @gildor The solution could also be used for
    onPermissionsRequestResult
    , which has been a real pain for Android developers, given the count of permissions helper libraries
    ➕ 1
    g
    p
    b
    • 4
    • 12
  • g

    gregd

    11/30/2017, 11:30 PM
    Hi all :kotlin: Has someone already noticed/reported that
    suspend
    doesn’t work with
    typealias
    ? Example:
    typealias MyType = (Any) -> Any
    fun testFunction(block: suspend MyType) {}
    //                      ^^^^^^^
    // Modifier 'suspend' is not applicable to 'non-functional type'
    d
    e
    • 3
    • 4
  • a

    arocnies

    12/01/2017, 4:31 AM
    Has anyone implemented a
    runBlocking {...}
    for Kotlin/JS?
    launch {...}
    is relatively straight forward but I just can't seem to figure out how to properly block for a coroutine in JS.
    g
    p
    • 3
    • 3
Powered by Linen
Title
a

arocnies

12/01/2017, 4:31 AM
Has anyone implemented a
runBlocking {...}
for Kotlin/JS?
launch {...}
is relatively straight forward but I just can't seem to figure out how to properly block for a coroutine in JS.
g

gildor

12/01/2017, 5:17 AM
I’m not sure that this is possible, because JS has only one thread (not including webworker) if you lock you cannot do anything else
p

pdvrieze

12/01/2017, 10:05 AM
@arocnies
runBlocking
doesn't do what you may think it does. It runs everything (except subcoroutines) on the same thread (the one that started it). In some ways this fits well with Javascript. You cannot actually wait for a coroutine completion in Javascript. What you do is use a callback on completion. Waiting for a coroutine amounts to either inside the coroutine just doing what you want with the result or starting a new coroutine that invokes await on the previous one.
g

gildor

12/01/2017, 10:26 AM
runBlocking still could be useful on JS, you can just lock main app thread and wait coroutine, but it’s just impossible to do, as I understand because of single thread nature of JS, because if you locked main thread you cannot do anything else, even get result of some async callback (from worker or network request)
View count: 4