Timeline for Implementing both Sync and Async clients with DRY
Current License: CC BY-SA 4.0
20 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Aug 5, 2023 at 10:54 | history | protected | gnat | ||
Oct 15, 2020 at 0:07 | history | bumped | CommunityBot | This question has answers that may be good or bad; the system has marked it active so that they can be reviewed. | |
Jun 16, 2020 at 23:02 | history | bumped | CommunityBot | This question has answers that may be good or bad; the system has marked it active so that they can be reviewed. | |
Feb 17, 2020 at 23:01 | history | bumped | CommunityBot | This question has answers that may be good or bad; the system has marked it active so that they can be reviewed. | |
Jan 19, 2020 at 9:59 | comment | added | Felix | @TheodorosChatzigiannakis That's really interesting, I haven't heard of that before! Thank you, I'll look into it. | |
Jan 19, 2020 at 9:11 | comment | added | Theodoros Chatzigiannakis | I think the abstraction you're looking for is the monad. As concrete example of abstracting over sync/async, you could check out ZIO from Scala. | |
Jan 18, 2020 at 22:13 | history | edited | Felix | CC BY-SA 4.0 |
added 290 characters in body
|
Jan 18, 2020 at 22:08 | comment | added | Felix |
@FilipMilovanović I think I'm closer to understanding. So given that I want to provide both sync and async alternatives, are you saying that even if refactoring them behind one function could be done like above, it's not advisable because it leads to confusion? I mean, if async_mode is false, then the functions are just regular synchronous functions, right? It is for use cases where asynchronous calls are beneficial that I want to provide the async alternative - returning awaitables instead of response contents directly.
|
|
Jan 18, 2020 at 18:46 | comment | added | Filip Milovanović | What Evan is saying is that it's not a good idea to provide synchronous wrappers for async functions because their async nature is not something client code can reasonably ignore (not a good idea to abstract the async away). There's limited/questionable benefit to doing it the other way around as well, so this leaves you with duplication, and again, that introduces problems but doesn't give you much in return. | |
Jan 18, 2020 at 16:59 | comment | added | Ewan | if you are making a request to a server the network round trip takes a long time. your code can either block the thread while it waits or not. async just exposes that choice to the calling code. Hiding that choice, doing the blocking for the calling code only makes sense if the person calling the method doesnt know how to block and wait# | |
Jan 18, 2020 at 16:15 | comment | added | Felix |
@Ewan I am a bit inexperienced with async, so correct if I'm wrong. When e.g. making more than one call to a server, is it not much faster to use async and gather the results rather than calling one by one? Maybe I don't just understand your point. sync_request and async_request in the examples above are of course blocking and non-blocking respectively.
|
|
Jan 18, 2020 at 16:15 | review | Close votes | |||
Feb 4, 2020 at 3:05 | |||||
Jan 18, 2020 at 16:12 | comment | added | Ewan | I mean the underlying function is either async or not, the calling code gets to decide if it wants to block until the result is returned or not. Wrapping an async within your library is just a bit pointless | |
Jan 18, 2020 at 16:08 | comment | added | gnasher729 | @Ewan Makes total sense if you want to call the method from a background thread. Synchronous calls are easier to handle.Of course the reason for "why do you want to provide X" is often "because customers demand X". | |
Jan 18, 2020 at 16:07 | comment | added | Felix | @Ewan What do you mean? To my knowledge, async awaitables can be returned from synchronous functions. I simply want to implement both async and sync interfaces with minimal effort and maintenance. | |
Jan 18, 2020 at 16:06 | answer | added | gnasher729 | timeline score: 1 | |
Jan 18, 2020 at 16:05 | comment | added | Ewan | why do you want to provide a non async wrapper for an async method? | |
Jan 18, 2020 at 16:05 | comment | added | Felix | I would much appreciate a comment on the downvote. Is this question not appropriate for SE? Perhaps it could be posted somewhere else. But it is not really a Code Review question either, definitely not Stack Overflow. | |
Jan 18, 2020 at 15:11 | history | edited | Felix | CC BY-SA 4.0 |
added 914 characters in body
|
Jan 18, 2020 at 14:54 | history | asked | Felix | CC BY-SA 4.0 |