Skip to content

Instantly share code, notes, and snippets.

@benzen
Last active August 29, 2015 14:17
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 benzen/25aa2729d8416493ad93 to your computer and use it in GitHub Desktop.
Save benzen/25aa2729d8416493ad93 to your computer and use it in GitHub Desktop.
Highland vs Async
async = require 'async'
findClient = (clientsColl, clientId, cb) ->
clientsColl.findOne _id: new ObjectID(clientId), cb
findProjetsByClient = (clientId, cb) ->
projetsModule.find client: new ObjectID(clientId), cb
joinClientAndProjetsIds = (client, projetsIds, cb) ->
client.projets = projetsIds
cb null, client
async.auto
clientsColl: (cb, ctx) -> getClientsColl cb
projets: (cb, ctx) -> findProjetsByClient clientId, cb
client: ["clientsColl", (cb, ctx) -> findClient ctx.clientsColl, clientId, cb]
projetsIds: ["projets", (cb, ctx) -> extractIds ctx.projets, cb ]
joinClientAndProjetsIds: ["client", "projetsIds", (cb, ctx) -> joinClientAndProjetsIds ctx.client, ctx.projetsIds, cb]
, (err, ctx) -> cb err, ctx?.joinClientAndProjetsIds
_ = require 'underscore'
async = require 'async'
oid = (id) -> new ObjectID id.toString()
getClientColl2 = (cb) ->
getClientColl cb
findProjetsByClient = (id) -> (cb) ->
projetsModule.find client: oid(id), cb
findClient = (id) -> (cb, {clientColl}) ->
clientsColl.findOne _id: oid(id), cb
extractProjetsIds = (cb, {projets} ) -> extractIds projets, cb
joinClientAndProjetsIds = (cb, {client, projetsIds}) ->
cb null, _.extend client, projets: projetsIds
async.auto
clientsColl: getClientsColl2
projets: findProjetsByClient(clientId)
client: ["clientsColl", findClient(clientId)]
projetsIds: ["projets", extractProjetsIds ]
joinClientAndProjetsIds: ["client", "projetsIds", joinClientAndProjetsIds]
, (err, ctx) -> cb err, ctx?.joinClientAndProjetsIds
_ = require "highland"
_(getClientsColl)
.map (clientsColl) -> _.wrapCallback(clientsColl.findOne)(_id: new ObjectID(clientId))
.flatten()
.map (client) ->
_.wrapCallback(projetsModule.find)(client: new ObjectID(clientId))
.map (projets) -> projets.map (p) -> p._id
.map (projetsIds) -> _.extend projets:projetsIds, client
.merge()
.errors (err) -> cb err
.each (clients) -> cb null, clients
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment