diff --git a/js/js.libraries/src/core/promise.kt b/js/js.libraries/src/core/promise.kt index 6e493578f9a..34ec68a2165 100644 --- a/js/js.libraries/src/core/promise.kt +++ b/js/js.libraries/src/core/promise.kt @@ -16,10 +16,19 @@ package kotlin.js +import kotlin.internal.LowPriorityInOverloadResolution + /** * Exposes the JavaScript [Promise object](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise) to Kotlin. */ public open external class Promise(executor: (resolve: (T) -> Unit, reject: (Throwable) -> Unit) -> Unit) { + @LowPriorityInOverloadResolution + public open fun then(onFulfilled: ((T) -> S)?): Promise + @LowPriorityInOverloadResolution + public open fun then(onFulfilled: ((T) -> S)?, onRejected: ((Throwable) -> S)?): Promise + + public open fun catch(onRejected: (Throwable) -> S): Promise + companion object { public fun all(promise: Array>): Promise> @@ -28,9 +37,20 @@ public open external class Promise(executor: (resolve: (T) -> Unit, rejec public fun reject(e: Throwable): Promise public fun resolve(e: S): Promise + public fun resolve(e: Promise): Promise } +} - public open fun then(onFulfilled: ((T) -> S)?, onRejected: ((Throwable) -> S)? = definedExternally): Promise +// It's workaround for KT-19672 since we can fix it properly until KT-11265 isn't fixed. +inline fun Promise>.then( + noinline onFulfilled: ((T) -> S)? +): Promise { + return this.unsafeCast>().then(onFulfilled) +} - public open fun catch(onRejected: (Throwable) -> S): Promise -} \ No newline at end of file +inline fun Promise>.then( + noinline onFulfilled: ((T) -> S)?, + noinline onRejected: ((Throwable) -> S)? +): Promise { + return this.unsafeCast>().then(onFulfilled, onRejected) +} diff --git a/js/js.libraries/test/core/PromiseTest.kt b/js/js.libraries/test/core/PromiseTest.kt new file mode 100644 index 00000000000..3c78884693c --- /dev/null +++ b/js/js.libraries/test/core/PromiseTest.kt @@ -0,0 +1,77 @@ +/* + * Copyright 2010-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package core + +import kotlin.js.Promise +import kotlin.test.* + +class PromiseTest { + // To be sure that some base cases can be written in Kotlin + fun smokeTest(p: Promise, ps: Promise) { + var _p: Promise + _p = p.then { + 1 + } + + _p = p.then({ + 1 + }) + + val f: (Int) -> Int = { 1 }; + val ft: (Throwable) -> Int = { 1 }; + + _p = p.then(f, ft); + + _p = p.then({ + 1 + } , { + 1 + }) + + _p = p.then({ + 1 + }) { + 1 + } + + _p = p.then(onFulfilled = { + 1 + }) + + _p = p.then(onFulfilled = { + 1 + }) { + 1 + } + + p.then { + ps + }.then { + var s: String = it + assertTrue((s as Any) is String) + ps + }.then( + { + var s: String = it + assertTrue((s as Any) is String) + }, + { + + } + ) + } +} \ No newline at end of file