kriskowal/q — pull request #571 by franksrevenge (2014-08-18 15:32:50+00:00)
Title: Data dispatch for Q

Hello,

Might this interest the Q community?

I deal with long promise chains in which subchains can be added outside of the original function scope, and there doesn't seem to be an easy way to share data across all these chains and subchains. Passing parameters from one function to another gets inelegant and increases a chance for human failure, since a single missed link in the chain invalidates the passed data for the rest of the chain.

[Clearer explanation from user's perspective](http://stackoverflow.com/questions/25270917/getting-and-setting-a-value-from-any-promise-in-a-q-js-promise-chain/25366258)

My proposal for a solution is a "local" data object, which is dispatched to all promises connected to a chain or its subchains and made accessible for reading and writing via `promise.local(function(localData) {})`.

Here's a simplified example of how it works at the moment:

```javascript
Q.all([a, b])
.local(function(localData) {
localData.myVariable = 'Hello world';
})
.all([a, b])
.all([c, d])
.then(function() {
return Q.all([c, d])
.local(function(localData) {
console.log(localData.myVariable);
});
});
```

Happy to refactor, if need be. Although fully functional and compatible with 1.x as is, this is just a conversation-opener.