diff --git a/CHANGELOG.md b/CHANGELOG.md index f356ff1c470..a8be02b7448 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 0.4.12 +* `Coroutines` + * Update `launchSynchronously` signature * `Selector` * Project created diff --git a/coroutines/src/jvmMain/kotlin/dev/inmo/micro_utils/coroutines/LaunchSynchronously.kt b/coroutines/src/jvmMain/kotlin/dev/inmo/micro_utils/coroutines/LaunchSynchronously.kt index c2a226e18be..bfd044534b5 100644 --- a/coroutines/src/jvmMain/kotlin/dev/inmo/micro_utils/coroutines/LaunchSynchronously.kt +++ b/coroutines/src/jvmMain/kotlin/dev/inmo/micro_utils/coroutines/LaunchSynchronously.kt @@ -2,12 +2,12 @@ package dev.inmo.micro_utils.coroutines import kotlinx.coroutines.* -fun launchSynchronously(scope: CoroutineScope = CoroutineScope(Dispatchers.Default), block: suspend CoroutineScope.() -> T): T { +fun CoroutineScope.launchSynchronously(block: suspend CoroutineScope.() -> T): T { var throwable: Throwable? = null var result: T? = null val objectToSynchronize = java.lang.Object() val launchCallback = { - scope.launch { + launch { safely( { throwable = it @@ -26,3 +26,5 @@ fun launchSynchronously(scope: CoroutineScope = CoroutineScope(Dispatchers.D } throw throwable ?: return result!! } + +fun launchSynchronously(block: suspend CoroutineScope.() -> T): T = CoroutineScope(Dispatchers.Default).launchSynchronously(block)