Javascript, function chaining and events?
Digging into NodeJS I've come across (fab) which really impressed me. After watching Jed's presentation I understand how function chaining works but开发者_开发问答 I'm left with one question: In case of a situation where an event can (but doesn't have to) occur such as in case of an HTTP listener, how does function chaining work?
For example, if I had the following pattern:
(listen, 8080)
(write)
('test1')
('test2')
()
()
How would I write the listen function that it in case of an HTTP request the strings 'test1' and 'test2' get written to the client?
For general solution to the problem of orchestration of async calls in javascript, please see https://github.com/tatumizer/mesh. It does parallelization and chaining automatically, based on knowledge of input and output parameters of each function. There's very detailed README file, with lots of examples.
I'm not sure how it is implemented in (fab) but something like what you explain can be implemented using promises and deferred objects. See the CommonJS, I Promise talk by Kris Kowal. See also Futures and promises on Wikipedia.
精彩评论