[英]Extending promise pattern?
Is it an anti pattern to extend promises with extra functions? 用额外的功能扩展承诺是一种反模式吗? I have this example of a service that does API calls to Facebook:
我有一个向API进行Facebook调用的服务的示例:
FacebookService
- importFeed(fanPage, sinceDate): Promise
- importComments(fanPage, recursively, sinceDate): Promise
- publishPost(fanPage): Promise
- checkApiStatus(): Promise
This could be rewritten to 这可以重写为
FacebookService
- importFeed(): ExtendedPromise
- importComments(recursively): ExtendedPromise
- publishPost(): ExtendedPromise
- checkApiStatus(): Promise
and used like that 像那样使用
var service = new FacebookService();
var fanPage = new FanPage(...);
service.importFeed()
.from(fanPage)
.since(new Date(...))
.then(fn, fn);
service.importComments(true)
.from(fanPage)
.then(fn, fn);
What are the cons of such an implementation? 这样的实现有什么弊端?
A promise is just an interface . 一个承诺只是一个接口 。 Any object can implement the API and as such provide promise functionality.
任何对象都可以实现API,从而提供承诺功能。
The disadvantage is that your code is less flexible/composable if you rely on the added functionality. 缺点是,如果您依赖于所添加的功能,则代码的灵活性/可组合性较差。 Other functions must know about and return such an "extended promise", they cannot just return any promise.
其他函数必须知道并返回这样的“扩展承诺”,它们不能仅仅返回任何承诺。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.