Skip to content

Commit

Permalink
Merge pull request #517 from arkivanov/add-Single-asPromise-extension
Browse files Browse the repository at this point in the history
Add Single.asPromise() extension
  • Loading branch information
CherryPerry authored Aug 9, 2020
2 parents f5c81e7 + 9669f32 commit 91d9bd0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.badoo.reaktive.single

import kotlin.js.Promise

fun <T> Single<T>.asPromise(): Promise<T> =
Promise { resolve, reject ->
subscribe(onSuccess = resolve, onError = reject)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.badoo.reaktive.single

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertSame

class AsPromiseTest {

@Test
fun resolves_non_null_value(): dynamic =
singleOf(1)
.asPromise()
.then { assertEquals(1, it) }

@Test
fun resolves_null_value(): dynamic =
singleOf<Int?>(null)
.asPromise()
.then { assertEquals(null, it) }

@Test
fun rejects(): dynamic {
val error = Exception()

return singleOfError<Nothing>(error)
.asPromise()
.catch { it }
.then { assertSame(error, it) }
}
}

0 comments on commit 91d9bd0

Please sign in to comment.