Hey all, I am getting a "doubled" promises in some...
# javascript
b
Hey all, I am getting a "doubled" promises in some of my code and I end up manually casting. Is there a way to avoid this anti-pattern?
Copy code
/** Convert promise with HTTP 500 response to failed promise. */
fun Promise<Response>.handleErrorCodes(): Promise<Response> = then { response ->
    if(response.ok) response
    else response.text().then { body ->
        throw RuntimeException("${response.status}: $body")
    }
}.then { it as Response }
t
JS would cast a a
Promise<Promise<T>>
automatically into
Promise<T>
. As Kotlin cannot do this, there is a extension function unwrapping it for you. but still you'd have to write
then {it}
yet, under the hood it still makes a type cast, but at least you dont see it 😄
b
Fair enough, I guess I'll just have to keep casting.