Skip to content

Instantly share code, notes, and snippets.

@qubyte
Last active December 29, 2015 17:59
Show Gist options
  • Save qubyte/7707533 to your computer and use it in GitHub Desktop.
Save qubyte/7707533 to your computer and use it in GitHub Desktop.
async.switch
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