Sobes.tech
Middle — Senior

Controlled asynchronous execution of sequential UseCase operations

livecode

Task condition

It is necessary to build a task execution mechanism following this sequence:

  • First, run FirstUseCase and pause execution until it completes successfully.
  • After confirming the success of FirstUseCase, start SecondUseCase and ThirdUseCase in parallel.
  • All other actions can be performed simultaneously without waiting.
  • Results (success/error) are handled through .onSuccess and .onFailure methods.
internal class TaskDelegate @Inject constructor(
    private val firstUseCase: UseCaseOne,
    private val repository: Storage,
    private val secondUseCase: UseCaseTwo,
    private val thirdUseCase: UseCaseThree,
    private val dataProcessor: DataManager,
) {
    fun executeTasks() {
        firstUseCase(UseCaseOne.Params())
            .onSuccess { repository.updateData() }

        dataProcessor.processData()

        secondUseCase(UseCaseTwo.Params(timeoutDuration))
            .onSuccess { repository.updateData() }

        thirdUseCase(Unit)
            .onSuccess {
                // something
            }
            .onFailure {
                // something
            }
    }
}