Skip to content

Instantly share code, notes, and snippets.

@Radagaisus
Created October 26, 2013 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Radagaisus/7166739 to your computer and use it in GitHub Desktop.
Save Radagaisus/7166739 to your computer and use it in GitHub Desktop.
parallel middlewares
async = require 'async'
# The `parallel` middleware allows to run several middlewares in parallel.
# It does this by simply using AsyncJS' `applyEach` on the middlewares.
#
# The middlewares will receive 3 parameters: `request`, `response` and
# `next`, in the same way and order they usually get them. As the
# execution is in parallel, if one of the middlewares will have an error
# the second one will continue to execute. If an error is passed to the
# `next` callback it will be passed correctly forward.
#
# Example usage:
#
# @get '/connect/facebook/callback',
# controllers.players.authentication.facebook,
# middlewares.parallel(
# controllers.players.authentication.update_gender_statistics,
# middlewares.background_jobs.queue
# )
#
# See AsyncJS docs for more info: https://github.com/caolan/async#applyEach
#
module.exports = parallel = (middlewares...) ->
return (req, res, next) ->
async.applyEach middlewares, req, res, (err) ->
next(error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment