원시값을 가지는 플로우

suspend fun main() {
    flowOf(1, 2, 3, 4, 5).collect { print(it) } // 12345
}

컨버터

suspend fun main() {
    listOf(1, 2, 3)
        .asFlow()
        .collect { print(it) } // 123
}

함수를 플로우로 바꾸기

suspend fun main() {
    val function = suspend { 
        delay(1000)
        "UserName"
    }
    
    function.asFlow().collect { print(it) } // (1초후) UserName
}

플로우 빌더

fun makeFlow(): Flow<Int> = flow {
    repeat(3) { num ->
        delay(1000)
        emit(num)
    }
}

suspend fun main() {
    makeFlow().collect { print(it) }
}

플로우 빌더 이해하기

public fun <T> flowOf(vararg element: T): Flow<T> = flow {
    for (element in elements) {
        emit(element)
    }
}