3 ms·
I vehemently oppose this proposal. The syntax of the proposed JavaScript addition is confusing and misleading, and will break more programs than JavaScript's un
by tomp 13y ago
I vehemently oppose this proposal. The syntax of the proposed JavaScript addition is confusing and misleading, and will break more programs than JavaScript's unfortunate `this`.
In particular, the following code snipped, copied from the blog post, goes against everything we've been taught by modern programming languages:
var a = future;
var b = a;
console.log(a,b); // will print 'hello' 'world'
b = 'world'; // this does not resolves 'a'
a = 'hello';
It breaks the concept of variables being references to objects. This is similar to Matlab's treatment of matrices, that none of it's successor languages (Python/NumPy, Julia) emulates.
Furthermore, it's confusing in the way that `a` could mean both (1) get value referenced by variable `a`, and (2) wait until value referenced by `a` is available. I believe that a much better proposal would be to treat futures as a separate data type (`a = future()`), and implement getting and fulfilling the future as method calls (`a.set(1)` and `a.wait()`).
- nawitus 13y agoI thought/interpreted that the code would stop executing on the line "var b = a", and continue outside the function scope. In fact, your a and b might never be resolved.
- IgorPartola 13y agoYour example illustrates the problem nicely. The code above is impossible to parse without knowing that "future" is a special value. Note that after you assign `a = future` you have no idea that a is special. Separate the code into three modules: the first contains lines 1 and 2, the second contains line 3 and the third contains lines 4 and 5. Now, looking at the second module you have no idea if the execution blocks on `console.log(a,b);` or not. More importantly, even if all the code is written as nicely as the example above you still cannot reason about this like you could about every other goddamn language out there. You can't pick arbitrary lines of code from the listing and execute them in that order, and if you try to figure out how it works (even if it's written without deadlocks) you will break your brain. Instead, let's something like $q [1], or even just the smaller/simpler AngularJS implementation of it [2]. While requiring callbacks, it's still very workable and does simplify things. Adding chained callbacks is very nice: var resp = $.get('/my/url'); showLoadingScreen(); resp.then(function(data) { return JSON.parse(data); } resp.then(function(data) { alert('data.username = ' + data.username); } resp.catch(function(err) { alert('Error: ' + err); }) resp.finally(function() { hideLoadingScreen(); }) [1] https://github.com/kriskowal/q/wiki/API-Reference https://github.com/kriskowal/q/wiki/API-Reference [2] https://docs.angularjs.org/api/ng/service/$q https://docs.angularjs.org/api/ng/service/$q
- tomp 13y agoThe point is, extending JS to support such constructs without requiring callbacks would be worthwhile, IMO [1]. However, making it obvious with a better API would be necessary. [1] Note, however, that it would turn JS's threading model completely on its head. Right now, the philosophy of JS is "single-process" threading model, with WebWorkers supporting a "multi-processing" concurrency model by isolating different threads of control. Allowing futures without callbacks would bring JS closer to Golang's concurrency model.