I was not aware of that coroutines work for JS, th...
# javascript
r
I was not aware of that coroutines work for JS, that's great 🙂 Following my initial example in pure JS:
Copy code
new Promise(function(resolve, reject) {
	resolve({
		ok: true, 
		text: function() {
			return new Promise(function(resolve, reject){ resolve("{json: 'body'}") })
		}
	})
}).then(function(response) {
	return response.text().then(function(text) {
		if (!response.ok) {
			reject('response was not ok...')
		}
		return text
	})
}).then(function(body){
	alert(body)
})
body isn't a promise in the last
then
but the string resolved from text()
g
As replacement for implicit conversion of nested promise as JS does using dynamic types you can write special function to unwrap nested promises explicitly:
Copy code
fun <T> Promise<Promise<T>>.unwrap() = then { it }
So it fixes your original sample Or even provide something like flatMap function for promises (like then + unwrap):
Copy code
// flatMap or for symmetry something like flatThen
fun <T, S> Promise<T>.flatMap(block: (T) -> Promise<S>): Promise<S> {
    return then {
        block(it)
    }.then { it }
}
So now to fix your sample you can replace first
then
with
flatMap
and avoid nested Promises Anyway, I would choose coroutines instead of chaining of promises