// see the complete implementation at // https://gist.github.com/objcode/7ab4e7b1df8acd88696cb0ccecad16f7 suspend fun cancelPreviousThenRun(block: suspend () -> T): T { // If there is an activeTask, cancel it because it's result is no longer needed activeTask?.cancelAndJoin() // ...