I would like to suspend a kotlin coroutine until a method is called from outside, just like the old Java object.wait() and object.notify() methods. How do I do that?

Here: [Correctly implementing wait and notify in Kotlin](https://stackoverflow.com/questions/44589669/correctly-implementing-wait-and-notify-in-kotlin) is an answer how to implement this with Kotlin threads (blocking). And here: [Suspend coroutine until condition is true](https://stackoverflow.com/questions/53906195/suspend-coroutine-until-condition-is-true) is an answer how to do this with CompleteableDeferreds but I do not want to have to create a new instance of CompleteableDeferred every time.

I am doing this currently:

```
    var nextIndex = 0

    fun handleNext(): Boolean {
        if (nextIndex < apps.size) {
            //Do the actual work on apps[nextIndex]
            nextIndex++
        }
        //only execute again if nextIndex is a valid index
        return nextIndex < apps.size
    }

    handleNext()

    // The returned function will be called multiple times, which I would like to replace with something like notify()
    return ::handleNext

```

From: <https://gitlab.com/SuperFreezZ/SuperFreezZ/blob/master/src/superfreeze/tool/android/backend/Freezer.kt#L69>