Extending promise pattern?

975 views Asked by At

Is it an anti pattern to extend promises with extra functions? I have this example of a service that does API calls to 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?

1

There are 1 answers

0
Felix Kling On BEST ANSWER

A promise is just an interface. Any object can implement the API and as such provide promise functionality.

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.