Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unwrapping the return Promise of then #15

Open
weironghuang31 opened this issue Aug 3, 2018 · 1 comment
Open

unwrapping the return Promise of then #15

weironghuang31 opened this issue Aug 3, 2018 · 1 comment

Comments

@weironghuang31
Copy link

Hey there,

kovenant is a great library. I love to use it. However, there is a small feature I like I wish you can provide is "unwrap the return Promise of then"

For example, I have two methods

fun method1() : Promise<String, Exception>{}
fun method2() : Promise<String, Exception>{}

// current
val p: Promise<Promise<String, Exception>, Exception> = method1().then{ method2() }
// can it just be 
val p: Promise<String, Exception> = method1().then{ method2() }

Now, I added these extensions in my project to achieve it, but I don't know whether it is a good way to do it and it is a little unclear to use thenFlat instead of then.

infix fun <V, R> Promise<V, Exception>.thenFlat(bind: (V) -> Promise<R, Exception>): Promise<R, Exception> {
    val deferred = deferred<R, Exception>(context)
    success {
        try {
            bind(it).success(deferred::resolve).fail(deferred::reject)
        } catch (e: Exception) {
            deferred.reject(ApiError(e))
        }
    }.fail(deferred::reject)
    
    return deferred.promise
}

// so it can be
val p: Promise<String, Exception> = method1().thenFlat{ method2() }
@denisftw
Copy link

@wadehuang36 I'm not sure about the core package, but you can use bind from kovenant-functional. I wish the function was called flatMap instead of bind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants