# I dont think i am using coroutines correctly here

**URL:** <https://discuss.kotlinlang.org/t/i-dont-think-i-am-using-coroutines-correctly-here/22494>\
**Category:** JavaScript\
**Created:** [July 23, 2021, 5:49pm UTC](https://discuss.kotlinlang.org/t/i-dont-think-i-am-using-coroutines-correctly-here/22494 "2021-07-23T17:49:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![huggy](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@huggy](https://discuss.kotlinlang.org/u/huggy)\
**Post date:** [July 23, 2021, 5:49pm UTC](https://discuss.kotlinlang.org/t/i-dont-think-i-am-using-coroutines-correctly-here/22494/1 "2021-07-23T17:49:39Z")

</div>

i want to be able to reuse this call to my server from a JS backend:

```auto
     suspend fun customerSummaryTwo(req: GenericIntEQ) : Deferred<CustomerPDSummaryDTO> {
        val deffered: Deferred<CustomerPDSummaryDTO> = GlobalScope.async{jsonClient.post<CustomerPDSummaryDTO>("http://localhost:8080/customerSummary"){
            contentType(ContentType.Application.Json)
            body = req
        }}
        return deffered
    }

```

and i am calling like this

```auto
attrs.onClickFunction = { evt ->
    GlobalScope.launch {
        val _req = GenericIntEQ(theInt = it.id)
        val firstRequest = PredictService.customerSummaryTwo(_req)
        val result = firstRequest.await()
        console.log("attrs.onClickFunction four", result)
    }
}

```

I think i am doing something wrong because i am using GlobalScope twice…

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [July 23, 2021, 7:46pm UTC](https://discuss.kotlinlang.org/t/i-dont-think-i-am-using-coroutines-correctly-here/22494/2 "2021-07-23T19:46:56Z")

</div>

You are right, it seems weird. The main point of suspend functions is to perform synchronous non-blocking operations. Making suspend function asynchronous (as you did) is usually a mistake.

What about this?

```auto
suspend fun customerSummaryTwo(req: GenericIntEQ) : CustomerPDSummaryDTO {
    return jsonClient.post<CustomerPDSummaryDTO>("http://localhost:8080/customerSummary"){
        contentType(ContentType.Application.Json)
        body = req
    }
}

```

And then just:

```auto
val result = PredictService.customerSummaryTwo(_req)

```

Note that you can do this only if `jsonClient.post()` is also suspend. I guess it actually is as it looks like Ktor client. If it wouldn’t be suspend, but blocking instead, you would need to wrap this call with `withContext(Dispatchers.IO) { }`.

---

<div class="post-metadata">

**Author:** ![huggy](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@huggy](https://discuss.kotlinlang.org/u/huggy)\
**Post date:** [July 23, 2021, 11:45pm UTC](https://discuss.kotlinlang.org/t/i-dont-think-i-am-using-coroutines-correctly-here/22494/3 "2021-07-23T23:45:04Z")

</div>

i will try it…

I was not usable data though. the working code is just what you have suggested with a callback param.

i am new to coroutines, and i am having issues jumping from JVM to JS variant.
