creationix/step

Can I preserve a callback across multiple steps?

NHQ opened this issue · 2 comments

NHQ commented

My first step is a two db queries in parallel (this.parallel)

My second step is to do something with the data from one of those queries, but not the other, which I want to preserve for the next step. I don't want to group them because they are not the same.

My third step is to render them both.

qv:

step(
    function step1(){
      person.findById(req.params.person, this.parallel());
      blurb.find({ref:req.params.person}, this.parallel());
    },
    function step2(err, person, blurbs){
    // refine blurb object, preserve person, and send them both to the next step
    },
    function step3(err, person, blurbs){
      if (err){console.log(err);}
      console.log(person);
      console.log(blurbs);
    });

There are several ways to do this.

(#1)
One would be to have your refine callback return a result object that contained both the person and the blurbs.

    { person: person, blurbs: refinedBlurbs }

This is probably the more correct method.

(#2)
You can also do

    this.person = person 

in the second step and then refer to this.person. I find that a bunch hackier though. It can be handy though if you're doing stuff in parallel with (I'm assuming) multiple blurbs.

(#3)
You can also run a 'parallel' process to your blurb refinement.
Then if you need a 'group' process for the blurb refinement you can also call step again inside of step

    var personCB = this.parallel();
    personCB(person);
    var firstStep = this;
    step(
      function () {
        //group blurb refinements
      },
      firstStep.parallel()
    )

In the new proposed API, there is a this.pass() function that passes values to the next step. Comment at #24