![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
Every time I use Java I find myself wondering whether the people in charge of its libraries are determined to make everything harder than necessary.
In JavaScript I can use promises and chain code together like so:
http.fetchSomething(myUrl)
.then(function(result){return saveTheResultAsynchronously(result);})
.then(function(furtherResult){displayTheResult(furtherResult);});
And provided the various methods return promises each "then" will be called once the previous bit of functionality has finished running, allowing them to be chained together really easily.
C# uses async/await for make things easy too.
Java, on the other hand, has "Future" as a type, which is the equivalent of a JS Promise - but one that doesn't allow for callbacks. Your options are to either poll "is it done yet?" repeatedly or to block the thread. There's no equivalent of "Then", which makes it really hard to run code when something finishes, let alone chain asynchronous methods together one after another.
You'd think that "Do X, and then when X is done, do Y" would be a basic requirement for any kind of asynchronous processing, but apparently the Java library creators disagree...
In JavaScript I can use promises and chain code together like so:
http.fetchSomething(myUrl)
.then(function(result){return saveTheResultAsynchronously(result);})
.then(function(furtherResult){displayTheResult(furtherResult);});
And provided the various methods return promises each "then" will be called once the previous bit of functionality has finished running, allowing them to be chained together really easily.
C# uses async/await for make things easy too.
Java, on the other hand, has "Future
You'd think that "Do X, and then when X is done, do Y" would be a basic requirement for any kind of asynchronous processing, but apparently the Java library creators disagree...
no subject
Date: 2014-10-13 07:45 am (UTC)You could do that sort of thing in BASIC on the Speccy, I'm astounded that something as well thought of as Java doesn't allow for it. I mean, 90% of modern coding might as well be Greek to me, but I know enough of the basics to be surprised when certain things are implemented weirdly in some languages.
no subject
Date: 2014-10-13 07:49 am (UTC)no subject
Date: 2014-10-13 08:01 am (UTC)OK, yeah, BASIC didn't allow for that, misunderstood, but, y'know, not my field and then some ;-)
no subject
Date: 2014-10-13 08:04 am (UTC)Basically, modern computers (a) have multiple cores, so they can do many things at once and (b) spend a lot of time waiting for the user, the internet, the hard disk, etc. so you want to make sure that you don't "block" waiting for one thing to finish. Writing code that can work in this way is hard, and lots of languages don't make it any easier, because the language creators didn't really think about it as hard as they should have done.