Created
January 17, 2014 20:47
-
-
Save raycmorgan/8481146 to your computer and use it in GitHub Desktop.
async.applyDeps
This file contains hidden or 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
// ---------------------------------------------------------------------------- | |
// Requirements | |
// ---------------------------------------------------------------------------- | |
var _ = require('underscore') | |
, async = require('async'); | |
// ---------------------------------------------------------------------------- | |
// Public functions | |
// ---------------------------------------------------------------------------- | |
/** | |
When using async.auto, appyDependencies allows you to apply the dependencies | |
to a function in a declaritive manner. | |
Usage: | |
User.get = function(id, callback) { ... } | |
Post.findAllByUser = function (user, callback) { ... } | |
async.auto({ | |
user: async.apply(User.get, 1), | |
posts: async.applyDeps(Posts.findAllByUser, 'user') | |
}, function (err, results) { | |
// results.user | |
// results.posts | |
}); | |
*/ | |
function applyDependencies(fn, depNames) { | |
depNames = _.toArray(arguments).slice(1); | |
return depNames.concat(function applyDependencies_(callback, results) { | |
try { | |
var dependencies = _.map(depNames, function (name) { | |
if (name in results) { | |
return results[name]; | |
} else { | |
throw new Error('Dependency was not met: ' + name + '. [' + _.keys(results) + ']'); | |
} | |
}); | |
fn.apply(this, dependencies.concat(_.toArray(arguments))); | |
} catch (err) { | |
callback(err); | |
} | |
}); | |
} | |
/** | |
Returns an async style function that wraps the given sync function. | |
Usage: | |
add(1, 2); // => 3 | |
var asyncAdd = wrap(add); | |
asyncAdd(1, 2, function (err, result) { | |
// result = 3 | |
}); | |
It also allows for partial eval when wrapping. | |
var asyncAddTwo = wrap(add, 2); | |
asyncAddTwo(3, function (err, result) { | |
// result 5 | |
}); | |
*/ | |
function wrap(fn) { | |
var outerArgs = _.toArray(arguments); | |
return function wrap_() { | |
var callback = _.last(arguments); | |
try { | |
callback(null, fn.apply(this, outerArgs.concat(_.initial(arguments)))); | |
} catch (e) { | |
callback(e); | |
} | |
}; | |
} | |
// ---------------------------------------------------------------------------- | |
// Async addons | |
// ---------------------------------------------------------------------------- | |
async.applyDependencies = applyDependencies; | |
async.applyDeps = applyDependencies; | |
async.wrap = wrap; |
This file contains hidden or 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
// Was | |
function processReport(report, callback) { | |
async.auto({ | |
githubInfo: async.apply(getGithubInformation, report.id), | |
report2: async.apply(markAsProcessing, report.id), | |
report3: ['report2', 'githubInfo', function (callback, results) { | |
fetchAndSetTreeSha(results.report2, results.githubInfo, callback); | |
}], | |
fileTree: ['report3', 'githubInfo', function (callback, results) { | |
fetchFileTree(results.report3, results.githubInfo, callback); | |
}], | |
report4: ['report3', 'fileTree', 'githubInfo', function (callback, results) { | |
fetchAndSaveFiles(results.report3, results.fileTree, results.githubInfo) | |
}] | |
}, callback); | |
} | |
// Using applyDeps | |
function processReport(report, callback) { | |
async.auto({ | |
githubInfo: async.apply(getGithubInformation, report.id), | |
report2: async.apply(markAsProcessing, report.id), | |
report3: async.applyDeps(fetchAndSetTreeSha, 'report2', 'githubInfo'), | |
fileTree: async.applyDeps(fetchFileTree, 'report3', 'githubInfo'), | |
report4: async.applyDeps(fetchAndSaveFiles, 'report3', 'fileTree', 'githubInfo') | |
}, callback); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment