Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The `request` method returns a promise, which you can call `then` on and pass in
### Enough Talk, Show Me Code
To make a request, you can do the following:

```
```javascript
var chn1 = postal.channel("user");

chn1.request({
Expand All @@ -39,7 +39,7 @@ chn1.request({

To handle requests:

```
```javascript
// SUCCESS REPLY
var subscription = chn1.subscribe("last.login", function(data, envelope) {
var result = getLoginInfo(data.userId);
Expand Down Expand Up @@ -67,7 +67,7 @@ That's up to you, actually. I have no desire to force another dependency on you.
Let's look at some examples:

####Using [jQuery](http://api.jquery.com/category/deferred-object/)
```
```javascript
// We need to tell postal how to get a deferred instance
postal.configuration.promise.createDeferred = function() {
return new $.Deferred();
Expand All @@ -79,7 +79,7 @@ postal.configuration.promise.getPromise = function(dfd) {
```

####Using [Q](https://github.com/kriskowal/q) (v0.9)
```
```javascript
// We need to tell postal how to get a deferred instance
postal.configuration.promise.createDeferred = function() {
return Q.defer();
Expand All @@ -91,7 +91,7 @@ postal.configuration.promise.getPromise = function(dfd) {
```

####Using [when.js](https://github.com/cujojs/when)
```
```javascript
// We need to tell postal how to get a deferred instance
postal.configuration.promise.createDeferred = function() {
return when.defer();
Expand All @@ -103,7 +103,7 @@ postal.configuration.promise.getPromise = function(dfd) {
```

####Using [rsvp](https://github.com/tildeio/rsvp.js/)
```
```javascript
// We need to tell postal how to get a deferred instance
postal.configuration.promise.createDeferred = function() {
return RSVP.defer();
Expand All @@ -114,6 +114,24 @@ postal.configuration.promise.getPromise = function(dfd) {
};
```

####Using [ES6](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
```javascript
// We need to tell postal how to get a deferred instance
postal.configuration.promise.createDeferred = function () {
var deferred = {};
deferred.promise = new Promise(function (resolve, reject) {
deferred.resolve = resolve;
deferred.reject = reject;
});
return deferred;
};

// We need to tell postal how to get a "public-facing"/safe promise instance
postal.configuration.promise.getPromise = function (deferred) {
return deferred.promise;
};
```

##How Does It work?
This is an add-on for [postal.js](https://github.com/postaljs/postal.js) - which is an in-memory message bus. The core behavior in postal is that publishers publish messages to which any number of susbcribers can listen (and subscribers never need a direct reference to the publisher(s)). These messages are "fire and forget". It's perfectly reasonable to set up your own "request/response" implementation using your own custom topics for both the request and response side of things, etc. In other words - you can achieve "request/response" behavior indirectly through "event/informational" messages, instead of "command/RPC" messages. However, there are times where having the clearly expressed intent of "request/response" is the best fit for the job - that's why I wrote this.

Expand Down