Last active
December 29, 2015 17:59
-
-
Save qubyte/7707533 to your computer and use it in GitHub Desktop.
async.switch
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
var async = require('async'); | |
// ASYNC SWITCH patches the async module with a switch like construct for asynchronous functions. | |
// Use like: | |
// async.switch(sitchedVar, { | |
// 'case 1': function (callback) {/*...*/}, | |
// 'case 2': function (callback) {/*...*/}, | |
// 'default': function (callback) {/*...*/} | |
// }, callback); | |
async.switch = function (switchVariable, functions, callback) { | |
var func = functions[switchVariable] || functions.default; | |
if (!func) { | |
return callback(new Error('No functions match the switch variable: ' + switchVar)); | |
} | |
func(callback); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment