在node.js 中创建链式方法?

发布于 2024-10-01 14:32:13 字数 143 浏览 1 评论 0原文

是否可以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

肩上的翅膀 2024-10-08 14:32:13

您基本上希望抽象 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.

顾挽 2024-10-08 14:32:13

当然,像任何 JavaScript 一样,您只需返回一个具有该方法的对象。

一种可能的布局(众多布局中的一种)。

var File = new (function() 
{ 
  this.create = function(str) 
  { 
    return this; 
  } 
  this.rename = function(str) 
  { 
    return this; 
  } 
})(); 

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; 
  } 
})(); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文