Javascript、函数链和事件?
在深入研究 NodeJS 时,我遇到了(fab),这给我留下了深刻的印象。观看 Jed 的演示后,我了解了函数链是如何工作的,但我留下了一个问题:如果事件可以(但不一定)发生,例如 HTTP 侦听器,那么函数链如何工作工作?
例如,如果我有以下模式:
(listen, 8080)
(write)
('test1')
('test2')
()
()
我将如何编写侦听函数,以便在 HTTP 请求的情况下将字符串“test1”和“test2”写入客户端?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有关 javascript 中异步调用编排问题的一般解决方案,请参阅 https://github.com/tatumizer/mesh 。它根据每个函数的输入和输出参数的知识自动进行并行化和链接。有非常详细的自述文件,有很多示例。
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.
我不确定它是如何在 (fab) 中实现的,但是像你所解释的那样可以使用承诺和延迟对象来实现。请参阅 Kris Kowal 的 CommonJS,我保证 演讲。另请参阅维基百科上的期货和承诺。
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.