Delay function in Async logic

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)
}

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.

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).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)

}

I’ve reformatted your code:

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:

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

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)
}
1 Like

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

1 Like

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
```

1 Like