4 ms·
Your 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
by IgorPartola 13y ago
Your 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.