Not so long ago began to learn JS, TypeScript, the node.js platform
Asynchronous I / O events and single-threaded is a great idea, but as a “classic” I am greatly confused by the resulting code complexity. That is, designs like:
network.receive(..., function(data) { if (data.command == 'logon') access.check('logon', function(result) { if (result == true) { log.add('call logon') db.sql('select ...', function(err, row) { log.add('logon success') network.send(data.address, 'success', function(res) { if (res...) ... }.bind(this)) }.bind(this)) } }.bind(this)) }.bind(this)) Here is the code of the ladder right annoying :) And these endless bind'y. Of course, I exaggerated the example, but I think the complaint is clear.
Maybe it is in me playing old habits? Is there any concept of writing code to make it more accurate, beautiful? For example, it is very unusual for me that if I wrote my class on TypeScript and call it something like this:
var result = new MyClass.search(...) That search () method will be called before the constructor finishes, if the constructor has to use asynchronous functions. That is, the constructor has not yet completed the initialization of the object, and someone already calls the object's methods. Is it possible to do something similar in the implementation (I spend an analogue with windows API):
search(...) { waitForSingleObject(event) ... } And until the constructor does its work and does not call: setEvent (event) then the execution of the search () method will slow down? At the same time, of course, waitForSingleObject () should be a smart function and make it clear to the thread that while you can do other things and not slow down, say, the entire node.js platform?
PS Sorry there are so many letters.