willconant / flow-js

Javascript Library for Multi-step Asynchronous Logic
MIT License
302 stars 15 forks source link

Sharing data between flow blocks #12

Closed yau-ref closed 6 years ago

yau-ref commented 9 years ago

There are some cases when it is necessary to pass data between the blocks of the flow.

Like there: http://stackoverflow.com/questions/4234619/how-to-avoid-long-nesting-of-asynchronous-functions-in-node-js or there:

function(){
  redisClient.smembers("names" this);
},function(err, names){
  redisClient.select(0,this);
},function(){
  redisClient.mget(/* `names` is needed here */, this);
}    

We have two options:

  1. Keep it in the shared scope - the most obvious and the worst way, since the value could be sensetive and shouldn't be accessed out of the flow.
  2. Assign it to this.varname - works well, but looks tricky and can cause unexpected behavior in certain circumstances.

So, I've implemented STASH and REVEAL methods for this case:

this.STASH("names", names)
..
names = this.REVEAL("names")