npm docs execute

Execute, executes multiple functions in parallel and aggregates the results
in a hash
that has the same keys as your functions did.

Don't use async, it's a kitchensink of junk patterns.

```js
var execute = require("execute")

function handler(req, res) {
  execute({
    one: function (cb) { queryOne("query", cb) }
    , two: function (cb) { queryTwo("query" cb) }
  }, function (err, results) {
    if (err) {
      return res.error(err)
    }

    var one = results.one
      , two = results.two

     res.render("tmpl", { one: one, two: two })
  })
}
```


On Wed, Dec 26, 2012 at 2:12 AM, klrumpf <klru...@gmail.com> wrote:

>  an example in coffeescript, don't know if it is the most elegant
> solution.
>
> You nest the two (three...) functions (q1...q2...qx) with your mysql
> statements having the
> first call the second (and the second the third...) and at the end you
> call the first (q1).
>
> That way they will get executed in the order determined by you (sync). You
> have a local
> "myRender" inherit the render method and call that in your function. That
> should be all.
>
> Alternatively use a module like async. (I try keep use of modules low to
> avoid extra overhead)
>
> ...
>     myRender = @render   # make render method available for funcs below
>
>     q1 = ->
>       MysqlConnection = mysql.createConnection(mysql_options)
>       MysqlConnection.connect()
>       # is there a matching user (pktnr) with this password (dance)?
>       sql = "select count(*) as pkt_count from pkt where pktnr = " +
> MysqlConnection.escape(suchpktnr) + " and dance = " +
> MysqlConnection.escape(suchpwd)
>       MysqlConnection.query sql, (err, result) ->
>         throw err if err
>         MysqlConnection.end
>         q2(result[0].pkt_count)
>
>     #nested callback, write sessionid to mysql if pktnr+pwd exists in db
>     q2 = (pkt_count) ->
>       if pkt_count > 0
>         pr_pktnr = suchpktnr
>         #if correct then write sessionid to the customers record in mysql
>         MysqlConnection = mysql.createConnection(mysql_options)
>         MysqlConnection.connect()
>         sql = "update pkt set sessionid = '" + pr_sid + "' where pktnr = "
> + MysqlConnection.escape(suchpktnr) + " and dance = " +
> MysqlConnection.escape(suchpwd)
>         MysqlConnection.query sql, (err, result) ->
>           throw err if err
>           MysqlConnection.end
>                 myRender showstock1: {
>             pktnr: pr_pktnr
>             currentBrowser: currentBrowser
>             }
>       else
>         pr_pktnr = '00000'
>         myRender showstock1: {
>           pktnr: pr_pktnr
>           currentBrowser: currentBrowser
>           }
>
>     q1()
>
> Hope this is useful, best wishes for the New Year
>
> Karl-L. rumpfklru...@gmail.com
> Málaga, Spain
>
>
> On 25/12/12 18:14, Joman Sierra wrote:
>
> I have this scenario:
>
>  function one
> {
> Makes a query to a big mysql table
>
>  }
>
>  Function two
> {
> Makes another query to a big myslq table
> }
>
>  res.render(showing results of the queries)
>
>
>  The problem is that when render is executed the mysql queries are still
> in progress.  What's the best way to be sure that the functions has
> finished before sending the render?
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nodejs@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
>  --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nodejs@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to