# Confused about Promises or otherwise suspended functions

**URL:** <https://discuss.kotlinlang.org/t/confused-about-promises-or-otherwise-suspended-functions/27839>\
**Category:** JavaScript\
**Created:** [December 23, 2023, 6:30pm UTC](https://discuss.kotlinlang.org/t/confused-about-promises-or-otherwise-suspended-functions/27839 "2023-12-23T18:30:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![SvenWollinger](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/svenwollinger/32/10122_2.png) [@SvenWollinger](https://discuss.kotlinlang.org/u/SvenWollinger)\
**Post date:** [December 23, 2023, 6:30pm UTC](https://discuss.kotlinlang.org/t/confused-about-promises-or-otherwise-suspended-functions/27839/1 "2023-12-23T18:30:19Z")

</div>

Hey, i got a function that requests a json file from a server, which i figured out how to do in a blocking way thanks to coroutines.  
Im now in the situation where id like to have it assigned directly in a class, but thats obviously outside of any coroutine scope.  
Is it not possible to directly do that?  
Like:

```auto
class Test {
val test = download("/test.json")
}

```

From what i can tell in pure javascript i could return a Promise and then await it, but the await function in Kotlin/js uses Coroutines, which then doesnt work since im not in a scope.  
Can someone please tell me if this is impossible or if im missing something obvious?  
Thanks!

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [December 23, 2023, 9:18pm UTC](https://discuss.kotlinlang.org/t/confused-about-promises-or-otherwise-suspended-functions/27839/2 "2023-12-23T21:18:12Z")

</div>

What you’re going to want to do instead is have a “smart constructor” that is a suspend function that does that for you. I’d recommend something like:

```kotlin
class Test(val test: Foo)
suspend fun Test() = Test(download("/test.json"))

```

---

<div class="post-metadata">

**Author:** ![SvenWollinger](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/svenwollinger/32/10122_2.png) [@SvenWollinger](https://discuss.kotlinlang.org/u/SvenWollinger)\
**Post date:** [December 24, 2023, 7:58am UTC](https://discuss.kotlinlang.org/t/confused-about-promises-or-otherwise-suspended-functions/27839/3 "2023-12-24T07:58:43Z")

</div>

I dont quite understand how that would allow me to assign the blolcking function to a variable directly.  
Ive found that Coroutines on JVM has “Future”, which seems like something that would help me here.  
Is there no equivilant in JS?

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [December 24, 2023, 9:02am UTC](https://discuss.kotlinlang.org/t/confused-about-promises-or-otherwise-suspended-functions/27839/4 "2023-12-24T09:02:54Z")

</div>

You can simply just use `runBlocking`, or instead you can wrap `Test()` in a [promise](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/%5Bjs%5Dpromise.html)

---

<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:** [December 24, 2023, 9:24am UTC](https://discuss.kotlinlang.org/t/confused-about-promises-or-otherwise-suspended-functions/27839/5 "2023-12-24T09:24:31Z")

</div>

It’s not entirely clear to me what you try to achieve.

> [@SvenWollinger](#):
>
> i figured out how to do in a blocking way thanks to coroutines

Coroutines don’t provide ways to write blocking code. It’s kind of opposite - coroutines allow to write the code that doesn’t block and they generally don’t like blocking.

Regarding object constructors, the simple answer is constructors support blocking, but don’t support suspending/coroutines. This is a technical limitation. Constructors are very special methods in many runtimes including JVM, so they can’t easily support running coroutines.

You can solve this by making `download()` a regular, non-coroutine/non-suspend blocking function. Also, you can “convert” a suspend function to a blocking function by using `runBlocking { download(...) }`. But generally, I advise against doing I/O operations inside a constructor, no matter with coroutines or without them. Constructor should be a relatively simple method with quick initialization, it shouldn’t perform too much CPU calculations nor I/O. I suggest the same as @kyay10 - use a factory function instead.

---

<div class="post-metadata">

**Author:** ![SvenWollinger](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/svenwollinger/32/10122_2.png) [@SvenWollinger](https://discuss.kotlinlang.org/u/SvenWollinger)\
**Post date:** [December 24, 2023, 9:29am UTC](https://discuss.kotlinlang.org/t/confused-about-promises-or-otherwise-suspended-functions/27839/6 "2023-12-24T09:29:11Z")

</div>

runBlocking is not available in kotlin js

---

<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:** [December 24, 2023, 9:32am UTC](https://discuss.kotlinlang.org/t/confused-about-promises-or-otherwise-suspended-functions/27839/7 "2023-12-24T09:32:33Z")

</div>

Ahh, ok, I don’t have much experience with compiling Kotlin to JS, but if it doesn’t at all allow to write blocking code (which makes sense for JS), then I guess your only option is a suspending factory function instead of calling `download()` inside a constructor.

---

<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:** [December 24, 2023, 9:48am UTC](https://discuss.kotlinlang.org/t/confused-about-promises-or-otherwise-suspended-functions/27839/8 "2023-12-24T09:48:57Z")

</div>

Also, you asked multiple times about promises/futures. Their equivalent in Kotlin is `Deferred`, however, I’m not exactly sure how they help here. If you meant to hold a future/promise/deferred in `val test` then yes, this is possible:

```kotlin
val test = GlobalScope.async { download("/test.json") } // or another scope

```

Of course then any function that accesses the value of `test` (they do: `test.await()`) has to be a suspend function. I personally still prefer a factory function.

---

<div class="post-metadata">

**Author:** ![SvenWollinger](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/svenwollinger/32/10122_2.png) [@SvenWollinger](https://discuss.kotlinlang.org/u/SvenWollinger)\
**Post date:** [December 24, 2023, 9:55am UTC](https://discuss.kotlinlang.org/t/confused-about-promises-or-otherwise-suspended-functions/27839/9 "2023-12-24T09:55:31Z")

</div>

Thank you, if you dont mind, how would i create such a factory function?

---

<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:** [December 24, 2023, 10:00am UTC](https://discuss.kotlinlang.org/t/confused-about-promises-or-otherwise-suspended-functions/27839/10 "2023-12-24T10:00:57Z")

</div>

@kyay10 provided an example in the first answer. We simply don’t run `download()` inside a constructor, but create an additional function for creating `Test` objects.
