# Delay function in Async logic

**URL:** <https://discuss.kotlinlang.org/t/delay-function-in-async-logic/29519>\
**Category:** Support\
**Created:** [November 13, 2024, 6:48pm UTC](https://discuss.kotlinlang.org/t/delay-function-in-async-logic/29519 "2024-11-13T18:48:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![knveraraju91](https://avatars.discourse-cdn.com/v4/letter/k/e9a140/32.png) [@knveraraju91](https://discuss.kotlinlang.org/u/knveraraju91)\
**Post date:** [November 13, 2024, 6:48pm UTC](https://discuss.kotlinlang.org/t/delay-function-in-async-logic/29519/1 "2024-11-13T18:48:04Z")

</div>

Hi please suggest me in the below code. It is not working. I am watching a YouTube video on callback program. It is working fine to presenter but not in my pgm. The issue is with delay function. The output expected to finish AMERICANO first even though LATTE is called before because of brew time is greater for Latte. Thank you

import kotlinx.coroutines.\*  
fun main(){  
val fred=Barista(“fred”)  
val sam=Barista(“sam”)

fred.accptOrder(CoffeeType.LATTE)  
sam.accptOrder(CoffeeType.AMERICANO)  
}

class Barista(val name:String):CoffeeMaker.OnCoffeeBrewedListner{  
private val coffeeMaker=CoffeeMaker()  
fun accptOrder(type:CoffeeType){  
coffeeMaker.brewCoffee(type, this)

}  
override fun onCoffeeBrewed(coffee:Coffee){  
println( “$name finshed brewing ${coffee.type}”)  
}  
}

data class Coffee(val type:CoffeeType)

class CoffeeMaker{  
fun brewCoffee(type:CoffeeType,callback:OnCoffeeBrewedListner){

delay(type.brewTime){

val madeCoffee=Coffee(type)  
callback.onCoffeeBrewed(madeCoffee)

}

}  
interface OnCoffeeBrewedListner{  
fun onCoffeeBrewed(coffee:Coffee)

}  
}

enum class CoffeeType(val brewTime:Long){

AMERICANO(brewTime= 300L),  
LATTE(brewTime=900L)  
}

---

<div class="post-metadata">

**Author:** ![ebrowne72](https://avatars.discourse-cdn.com/v4/letter/e/85f322/32.png) [@ebrowne72](https://discuss.kotlinlang.org/u/ebrowne72)\
**Post date:** [November 13, 2024, 10:32pm UTC](https://discuss.kotlinlang.org/t/delay-function-in-async-logic/29519/2 "2024-11-13T22:32:46Z")

</div>

Your code is very hard to read, could you please format it?

Also I don’t see you using coroutines at all. You have no suspend functions, and you aren’t launching anything.

---

<div class="post-metadata">

**Author:** ![knveraraju91](https://avatars.discourse-cdn.com/v4/letter/k/e9a140/32.png) [@knveraraju91](https://discuss.kotlinlang.org/u/knveraraju91)\
**Post date:** [November 13, 2024, 10:48pm UTC](https://discuss.kotlinlang.org/t/delay-function-in-async-logic/29519/3 "2024-11-13T22:48:04Z")

</div>

import kotlinx.coroutines.\*

fun main() {  
val fred = Barista(“fred”)  
val sam = Barista(“sam”)  
runBlocking {  
fred.accptOrder(CoffeeType.LATTE)  
sam.accptOrder(CoffeeType.AMERICANO)  
}  
}

class Barista(val name: String) : CoffeeMaker.OnCoffeeBrewedListner {  
private val coffeeMaker = CoffeeMaker()  
fun accptOrder(type: CoffeeType) {  
coffeeMaker.brewCoffee(type, this)

```
}

override fun onCoffeeBrewed(coffee: Coffee) {
    println("$name finshed brewing ${coffee.type}")
}

```

}

data class Coffee(val type: CoffeeType)

class CoffeeMaker {  
fun brewCoffee(type: CoffeeType, callback: OnCoffeeBrewedListner) {  
CoroutineScope([Dispatchers.IO](http://Dispatchers.IO)).launch {

```
        delay(type.brewTime)

        val madeCoffee = Coffee(type)
        withContext(Dispatchers.Main) {
            callback.onCoffeeBrewed(madeCoffee)

        }

    }

}

interface OnCoffeeBrewedListner {
    fun onCoffeeBrewed(coffee: Coffee)

}

```

}

enum class CoffeeType(val brewTime: Long) {

```
AMERICANO(brewTime = 300L),
LATTE(brewTime = 900L)

```

}

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [November 13, 2024, 11:06pm UTC](https://discuss.kotlinlang.org/t/delay-function-in-async-logic/29519/4 "2024-11-13T23:06:39Z")

</div>

I’ve reformatted your code:

```run-kotlin
import kotlinx.coroutines.*

fun main(){
    val fred=Barista("fred")
    val sam=Barista("sam")
    runBlocking{
        fred.accptOrder(CoffeeType.LATTE)
        sam.accptOrder(CoffeeType.AMERICANO)
    }
}

//Barista starts here
class Barista(val name:String):CoffeeMaker.OnCoffeeBrewedListner{
    private val coffeeMaker=CoffeeMaker()
    fun accptOrder(type:CoffeeType){
        coffeeMaker.brewCoffee(type, this)

    }
    override fun onCoffeeBrewed(coffee:Coffee){
        println( "$name finshed brewing ${coffee.type}")
    }
}

data class Coffee(val type:CoffeeType)

//Coffeemaker starts here
class CoffeeMaker{
    fun brewCoffee(type:CoffeeType,callback:OnCoffeeBrewedListner){
        CoroutineScope(Dispatchers.IO).launch{

            delay(type.brewTime)

            val madeCoffee=Coffee(type)
            withContext(Dispatchers.Main){
                callback.onCoffeeBrewed(madeCoffee)
        
            }
        }
    }
    interface OnCoffeeBrewedListner{
        fun onCoffeeBrewed(coffee:Coffee)
    }
}

//Coffeetype starts here
enum class CoffeeType(val brewTime:Long){

    AMERICANO(brewTime= 300L),
    LATTE(brewTime=900L)
}

```

And in fact I see no output from it, likely because you’re launching coroutines on `Dispatchers.IO` without waiting for them. Modifying your code lightly, I obtain this:

```run-kotlin
import kotlinx.coroutines.*

fun main(){
    val fred=Barista("fred")
    val sam=Barista("sam")
    runBlocking{
        fred.accptOrder(CoffeeType.LATTE)
        sam.accptOrder(CoffeeType.AMERICANO)
        delay(1200L)
    }
}

//Barista starts here
class Barista(val name:String):CoffeeMaker.OnCoffeeBrewedListner{
    private val coffeeMaker=CoffeeMaker()
    fun accptOrder(type:CoffeeType){
        coffeeMaker.brewCoffee(type, this)

    }
    override fun onCoffeeBrewed(coffee:Coffee){
        println( "$name finshed brewing ${coffee.type}")
    }
}

data class Coffee(val type:CoffeeType)

//Coffeemaker starts here
class CoffeeMaker{
    fun brewCoffee(type:CoffeeType,callback:OnCoffeeBrewedListner){
        CoroutineScope(Dispatchers.IO).launch{

            delay(type.brewTime)

            val madeCoffee=Coffee(type)
            withContext(Dispatchers.Default){
                callback.onCoffeeBrewed(madeCoffee)
        
            }
        }
    }
    interface OnCoffeeBrewedListner{
        fun onCoffeeBrewed(coffee:Coffee)
    }
}

//Coffeetype starts here
enum class CoffeeType(val brewTime:Long){

    AMERICANO(brewTime= 300L),
    LATTE(brewTime=900L)
}

```

and the behaviour works as expected.  
Likely though this is the code you want, where instead of callbacks, you use suspend

```run-kotlin
import kotlinx.coroutines.*

fun main(){
    val fred=Barista("fred")
    val sam=Barista("sam")
    runBlocking{
        launch { fred.accptOrder(CoffeeType.LATTE) }
        launch { sam.accptOrder(CoffeeType.AMERICANO) }
    }
}

//Barista starts here
class Barista(val name:String){
    private val coffeeMaker=CoffeeMaker()
    suspend fun accptOrder(type:CoffeeType){
        val coffee = coffeeMaker.brewCoffee(type)
        println( "$name finshed brewing ${coffee.type}")
    }
}

data class Coffee(val type:CoffeeType)

//Coffeemaker starts here
class CoffeeMaker{
    suspend fun brewCoffee(type:CoffeeType): Coffee {
        return withContext(Dispatchers.IO) {
            delay(type.brewTime)
            Coffee(type)
        }
    }
}

//Coffeetype starts here
enum class CoffeeType(val brewTime:Long){

    AMERICANO(brewTime= 300L),
    LATTE(brewTime=900L)
}

```

---

<div class="post-metadata">

**Author:** ![knveraraju91](https://avatars.discourse-cdn.com/v4/letter/k/e9a140/32.png) [@knveraraju91](https://discuss.kotlinlang.org/u/knveraraju91)\
**Post date:** [November 13, 2024, 11:10pm UTC](https://discuss.kotlinlang.org/t/delay-function-in-async-logic/29519/5 "2024-11-13T23:10:58Z")

</div>

Thank you very much. Sorry to ask you this. Which icon I need to use to paste code the same format as you did.

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [November 13, 2024, 11:59pm UTC](https://discuss.kotlinlang.org/t/delay-function-in-async-logic/29519/6 "2024-11-13T23:59:21Z")

</div>

It’s the icon that says “preformatted text”, should be right next to the quotation mark icon. Alternatively, simply surround your code in triple backticks:  
```  
code goes here  
```
