在node.js 中创建链式方法?
是否可以在 Node.js 中创建像这样的异步链式方法
File.create('file.jpg').rename('renamed.jpg').append('Hello World')
,即非阻塞。
Is it possible to create chained methods that are asynchronous like this in node.js
File.create('file.jpg').rename('renamed.jpg').append('Hello World')
That is to say non-blocking.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您基本上希望抽象 API 上文件处理操作的异步性质。
可以做到,我建议您看一下以下文章:
本文由 Dustin Diaz 撰写,他目前从事 @anywhere JavaScript API 工作,他完全按照您的意愿行事,使用简单的队列实现,可以创建一个独立于任何回调的流畅接口。
异步性是隐藏的,由 API 在内部处理,这是一种很好且简单的技术。
You basically want to abstract the asynchronous nature of the file-handling operations on your API.
It can be done, I would recommend you to give a look to the following article:
The article was written by Dustin Diaz, who currently works on the @anywhere JavaScript API, and he does exactly what you want, using a using a simple Queue implementation, a fluent interface can be created, being independent of any callback.
The asynchronicity is hidden and it is handled internally by your API, it's a nice and simple technique.
当然,像任何 JavaScript 一样,您只需返回一个具有该方法的对象。
一种可能的布局(众多布局中的一种)。
Sure, like any JavaScript, you just return an object that has that method.
One possible layout (among many).