title: Returning value from normal function which called suspend function using Kotlin Coroutine
body: <p>Hi I am using Kotlin coroutine library in a project.</p>
  
  <p>The below method calls a suspend function which return a boolean value.</p>
  
  <pre><code>fun isNetworkAvailable(context: Context?): Boolean {
              //return checkNetworkReachability(context)
              var isNetworkAvailable = false
              GlobalScope.launch(Dispatchers.Default) {
                  isNetworkAvailable = GlobalScope.async&lt;Boolean&gt; {
                      checkNetwork()
  
                  }.await()
              }
              return isNetworkAvailable
  
          }
  </code></pre>
  
  <p>Here checkNetwork is the suspend function. Before executing it the return value is passed to the caller (View/Activity).
  How could I achieve without making the "isNetworkAvailable" as suspend?.</p>
  
  <p>Inside the checkNetwork method, to check reach-ability calling network call as like below.</p>
  
  <pre><code>private suspend fun checkNetwork() : Boolean {
              val value = GlobalScope.async&lt;Boolean&gt; {
                  val isEastReachable = async { checkEastReachable() }
                  if (!isEastReachable.await()) {
                      checkWestReachable()
                  } else {
                      true
                  }
              }
  
              return value.await()
          }
  </code></pre>
  
  <p>And the sub-methods are </p>
  
  <pre><code>private suspend fun checkEastReachable(): Boolean = coroutineScope {
  
                  withContext(Dispatchers.Default) {
                      repository.networkManager.callReachableEast()
                  }
              }
  
  
  
  private suspend fun checkWestReachable(): Boolean = coroutineScope {
  
                  withContext(Dispatchers.Default) {
                      repository.networkManager.callReachableWest()
                  }
              }
  </code></pre>
  
  <p>The sub-suspend methods are calling a web service using retrofit. As it would return a boolean, I made it as an synchronous .execute() call.</p>
  
  <pre><code>fun callReachableEast(): Boolean {
          return try {
              val requestCall =
                  ApiService.create("eastApi").getReachabilityEast()
              requestCall.execute().isSuccessful
          } catch (exception: Exception) {
              false
          }
      }
  
      fun callReachableWest(): Boolean {
          return try {
              val requestCall =
                  ApiService.create("westApi").getReachabilityWest()
              return requestCall.execute().isSuccessful
          } catch (exception: Exception) {
              false
          }
      }
  </code></pre>
  
  <p>I have gone through the below links</p>
  
  <p><a href="https://kotlinlang.org/docs/reference/coroutines/composing-suspending-functions.html" rel="nofollow noreferrer">https://kotlinlang.org/docs/reference/coroutines/composing-suspending-functions.html</a></p>
  
  <p><a href="https://proandroiddev.com/async-operations-with-kotlin-coroutines-part-1-c51cc581ad33" rel="nofollow noreferrer">https://proandroiddev.com/async-operations-with-kotlin-coroutines-part-1-c51cc581ad33</a></p>
  
  <p>and some more.</p>
  
  <p>Repeating my question, How could I achieve without making the "isNetworkAvailable" as suspend?.</p>