Last active
August 29, 2015 14:17
-
-
Save benzen/25aa2729d8416493ad93 to your computer and use it in GitHub Desktop.
Highland vs Async
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_ = 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_ = 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