question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deferred has no map function

See original GitHub issue

there should be a function

Deferred<R>.map(map:(R)->T) : Deferred<T>

it is easy to implement yourself, but it is a bit confusing that it is missing. there might be other functions to consider, like

Deferred<Deferred<T>>.flatten() : Deferred<T>

or

List<Deferred<T>>.combine() : Deferred<List<T>>

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

26reactions
jcornazcommented, Oct 26, 2018

Le me add a note explaining why suspending function are preferable to functions returning Futures (incl. Deferred, Job, and others):

Marking a function suspend in Kotlin is semantically the same as making the function returning a future in Java. A suspending function IS a callback function. The only difference, is that the callback mechanism is hidden by the compiler, and we don’t actually see the callback, which make the code clearer. But it is a callback.

So, the whole point of Kotlin coroutines is to not use callback and future anymore. Instead we use suspending functions which lets us write code like sequential code while keeping the benefits of future and callbacks.

Therefore declaring a function fun foo(): Deffered<Int> instead of suspend fun foo(): Int is basically loosing the whole point of Kotlin coroutines. This could be done with any future library.

Consider this Java code:

public CompletableFuture<Integer> foo() { ... }
public CompletableFuture<Integer> bar(int arg) { ... }
public CompletableFuture<Object> usage() {
	return foo().thenCompose(x -> bar(x)).thenApply(it -> it + 1);
}

In this code we have to use Future, combine, map etc. or, because there is no better choice.

But the point of kotlin coroutines is to provide the suspend keyword in the language which hide all the complexity.

So here is the Kotlin idiomatic equivalent of the Java code above:

suspend fun foo(): Int { ... }
suspend fun bar(arg: Int) { ... }
suspend fun usage() = bar(foo()) + 1
12reactions
elizarovcommented, Aug 20, 2018

@SolomonSun2010 Thank. We’ve studied Dart design while working on Kotlin coroutines and we had put quite a lot of thought into making sure we don’t fall into the same futures trap as Dart did. As a result, the Dart example you’ve given looks much simpler (and nicer) in Kotlin:

suspend fun foo(): Int { ... }
suspend fun bar(arg: Int) { ... }
suspend fun usage() = bar(foo())
Read more comments on GitHub >

github_iconTop Results From Across the Web

data.map is not a function
The answer is to use data.products.map , not to wrap data in an array. Your answer causes the entire data object to be...
Read more >
TypeError: object.map is not a function in JavaScript
The "object.map is not a function" error occurs because the map method is not implemented on objects. To iterate over an object, use...
Read more >
Mapping on Deferred Contexts - Windows drivers
The runtime can map a dynamic resource (through a call to the driver's ResourceMap function) on a deferred context, as the Direct3D version ......
Read more >
A deferred problem or something else?
I am passing in a single query to execute, but it seems to execute for every key for every key. If there are...
Read more >
Map | API Reference | ArcGIS API for JavaScript 3.42
The load event is fired after the first layer has been added to the map. At this point, the map is fully functional....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found