chaining API vs promises

94 views
Skip to first unread message

Alex Kocharin

unread,
Sep 23, 2014, 8:34:06 PM9/23/14
to nod...@googlegroups.com

There seem to be a conflict between chaining api and promise-based api. Both of them use return value, but chaining api returns `this`, and promise-based one returns promise.

Is it possible to use both at the same time?

Here is some abstract example. Suppose we have a class:

```js
function Message(text) {
var self = Object.create(Message.prototype)
self.text = text
return self
}

Message.prototype.send = function(server, callback) {
servers[server].doStuff(callback)
return this
}
```

Which can be used this way:

```js
Message("text")
.send("server1", function() { console.log("message sent to server 1") } )
.send("server2", function() { console.log("message sent to server 2") } )
```

Some users want to use promises. Ideally, it'll look like that:

```js
Message("text")
.send("server1")
.then( function() { console.log("message sent to server 1") } )
.send("server2")
.then( function() { console.log("message sent to server 2") } )
```

I'm sure it's possible with prototype injection into promise, but it surely sounds weird. Is there a better way?

--
// alex

Owen Rubel

unread,
Mar 31, 2015, 11:11:24 PM3/31/15
to nod...@googlegroups.com, al...@kocharin.ru
An API chain is TECHNICALLY a complete monad wherein one request/response is made and the output is sent back. The chain is a dynamic stateless chain of apis sent as a a monad processed on the backend through a push/pull mechanism (like loopback or a HandlerInterceptor). Thus requests and responses are minimized and thus the term 'chain' is derived.

Making constant request/response calls over and over and over does not constitute an api chain. See docs on the API Chaining pattern (https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=api%20chaining)
Reply all
Reply to author
Forward
0 new messages