In the JavaScript std library, the `then` method o...
# javascript
n
In the JavaScript std library, the
then
method of the Promise class supports mapping, but not flatmapping. Calling
then
with a function of type (T)->Promise<S> should return a Promise<S>, but instead it returns a Promise<Promise<S>>. This makes it impossible to use the Fetch API.
k
Can you provide a code sample? I can't understand what's your problem
Ah, I see. When a JavaScript function passed to
then
returns a promise it's not passed forward as is, but gets resolved first.
I'm afraid it's impossible to represent in Kotlin type system.
n
Even with overloading?
k
Yes, even with overloading
n
It looks like we need some annotations to control the compilation to JS , like the
@JvmName
annotation on the JVM platform
So that Promise in Kotlin could have map and flatMap methods that are both translated to
then
in JS
k
There's
@JsName
annotation
It's possible to declare two functions with different names in
Promise
, say
then
and
thenPromise
, though it seems a bit ugly
n
The two behaviours are map & flatMap - it would be nice if they had the same names as used elsewhere in the Kotlin libraries
I tried using @JsName and was able to map map and flatMap to the then function.
Hmmm… is there no way to add a snippet to a thread?
Can you see this program on try.kotlinlang.org?
I had some trouble with mapError and flatMapError, and so had to hack around the type system. I’ll ask about that on the #general channel