growable latch helped: ``` private class Growabl...
# kovenant
a
growable latch helped:
Copy code
private class GrowableCountdownLatch {
        private val growableCount = AtomicInteger(0)
        private val latch = CountDownLatch(1)

        public fun increment() {
            growableCount.incrementAndGet()
        }

        public fun decrement() {
            if (growableCount.decrementAndGet() == 0) {
                latch.countDown()
            }
        }

        public fun await() {
            if (growableCount.get() == 0) return
            latch.await()
        }
    }
used with the promises:
Copy code
// "work" is concurrent queue

fun thatAddsWork(thing) {
   latch.increment()
   work.add(async {
       ... do work, also calls thatAddsWork()
   }.always {
       latch.decrement()
  })
}

latch.await()
all(work.toList()).then {
   ...
}
it blocks in latch.await() though, if you want all non blocking this isnt' for you