admin 管理员组文章数量: 1086019
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.
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.
Share Improve this question edited Mar 23, 2011 at 15:41 700 Software 88k88 gold badges242 silver badges347 bronze badges asked Nov 8, 2010 at 6:13 ajsieajsie 79.9k110 gold badges284 silver badges387 bronze badges 2- If it's non-blocking, shouldn't there be a callback function passed in somewhere? – Matthew Flaschen Commented Nov 8, 2010 at 6:31
- @Matthew: Yeah it should. So I wonder if there is some way to create a method chaining that is asynch. Maybe with a library that could handle it automatically somehow. – ajsie Commented Nov 8, 2010 at 6:48
2 Answers
Reset to default 8You basically want to abstract the asynchronous nature of the file-handling operations on your API.
It can be done, I would remend you to give a look to the following article:
- Asynchronous method queue chaining in JavaScript
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.
Sure, like any JavaScript, you just return an object that has that method.
One possible layout (among many).
var File = new (function()
{
this.create = function(str)
{
return this;
}
this.rename = function(str)
{
return this;
}
})();
本文标签: javascriptCreate chained methods in nodejsStack Overflow
版权声明:本文标题:javascript - Create chained methods in node.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1743993747a2515205.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论