I am looking for the equivalent of the command @escaping for kotlin if it exists?
func fetch(response: @escaping (PriceResponse?) -> Void)
I am looking for the equivalent of the command @escaping for kotlin if it exists?
func fetch(response: @escaping (PriceResponse?) -> Void)
Yes. exists. Also The syntax is similar to swift:
fun fetch(callback: (PriceResponse) -> Unit){ }
change PriceResponse
to whatever you're returning
Also edited
Then change your function invocation from fetch()
to
fetch {
callingYourAPI(here) // or you can get named argument
// do some more stuff
}
If your question is about removing the restriction of calling @escaping
function inside the fetch()
function - then you don't need to do anything special in Kotlin - just define the function as incoming argument.
Some more theory:
callsInPlace
contract.See for more info: https://stackoverflow.com/a/65989221/3134602
I think that according to this https://kotlinlang.org/docs/reference/lambdas.html all closures in kotlin are nonescaping and there is no way to make escaping.
UPD: Similar functionality can be achieved with help of kotlin corutines. More you can find here: https://kotlinlang.org/docs/reference/coroutines.html