diff --git a/README.md b/README.md index 735d08f..66f39ca 100644 --- a/README.md +++ b/README.md @@ -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({ @@ -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); @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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.