lib/commandCenter/commandCenter.js
function runCommand(output, cache, cmdIndex) {
				var cmd = batch.commands[cmdIndex];
				if (cmd) {
					if (cacheContent.length !== 0 && isCachedCommand(cmd)) {
						logger.warning
							.data({ cmd: cmd.cmdName })
							.log('Re-sending cached command response');

						output += (output ? ',' : '[') + cacheContent.shift();
						return setImmediate(runCommand, output, cache, cmdIndex + 1);
					}

					return that.executeCommand(cmd, session, metaData, function (error, response) {
						if (error) {
							// should never happen
							return cb(error);
						}

						if (headerEvents && headerEvents.length) {
							response.myEvents = response.myEvents || [];
							response.myEvents = headerEvents.concat(response.myEvents);
							headerEvents = null;
						}

						const serializedResponse = serializeCommandResult(response);
						output += (output ? ',' : '[') + serializedResponse;

						if (isCachedCommand(cmd)) {
							cache += (cache ? ',' : '[') + JSON.stringify(serializedResponse);
						}

						setImmediate(runCommand, output, cache, cmdIndex + 1);
					});
				}

				// close the output JSON array

				output += ']';

				if (cache === '') {
					cache = '[]';
				} else {
					cache += ']';
				}

				// turn results array into a reportable response (possibly gzipped)

				var options = {
					mimetype: CONTENT_TYPE_JSON
				};

				if (!shouldCompress(output, acceptedEncodings)) {
					return respond(options, output, cache);
				}

				return compress(output, function (error, compressed) {
					if (error) {
						// error during compression, send the original
						respond(options, output, cache);
					} else {
						options.encoding = 'gzip';

						respond(options, compressed, cache);
					}
				});
			}
function runCommand(output, cache, cmdIndex) {
				var cmd = batch.commands[cmdIndex];
				if (cmd) {
					if (cacheContent.length !== 0 && isCachedCommand(cmd)) {
						logger.warning
							.data({ cmd: cmd.cmdName })
							.log('Re-sending cached command response');

						output += (output ? ',' : '[') + cacheContent.shift();
						return setImmediate(runCommand, output, cache, cmdIndex + 1);
					}

					return that.executeCommand(cmd, session, metaData, function (error, response) {
						if (error) {
							// should never happen
							return cb(error);
						}

						if (headerEvents && headerEvents.length) {
							response.myEvents = response.myEvents || [];
							response.myEvents = headerEvents.concat(response.myEvents);
							headerEvents = null;
						}

						const serializedResponse = serializeCommandResult(response);
						output += (output ? ',' : '[') + serializedResponse;

						if (isCachedCommand(cmd)) {
							cache += (cache ? ',' : '[') + JSON.stringify(serializedResponse);
						}

						setImmediate(runCommand, output, cache, cmdIndex + 1);
					});
				}

				// close the output JSON array

				output += ']';

				if (cache === '') {
					cache = '[]';
				} else {
					cache += ']';
				}

				// turn results array into a reportable response (possibly gzipped)

				var options = {
					mimetype: CONTENT_TYPE_JSON
				};

				if (!shouldCompress(output, acceptedEncodings)) {
					return respond(options, output, cache);
				}

				return compress(output, function (error, compressed) {
					if (error) {
						// error during compression, send the original
						respond(options, output, cache);
					} else {
						options.encoding = 'gzip';

						respond(options, compressed, cache);
					}
				});
			}
function runCommand ( output , cache , cmdIndex ) { var cmd = batch . commands [ cmdIndex ] ; if ( cmd ) { if ( cacheContent . length !== 0 && isCachedCommand ( cmd ) ) { logger . warning . data ( { cmd : cmd . cmdName } ) . log ( 'Re-sending cached command response' ) ; output += ( output ? ',' : '[' ) + cacheContent . shift ( ) ; return setImmediate ( runCommand , output , cache , cmdIndex + 1 ) ; } return that . executeCommand ( cmd , session , metaData , function ( error , response ) { if ( error ) { // should never happen return cb ( error ) ; } if ( headerEvents && headerEvents . length ) { response . myEvents = response . myEvents || [ ] ; response . myEvents = headerEvents . concat ( response . myEvents ) ; headerEvents = null ; } const serializedResponse = serializeCommandResult ( response ) ; output += ( output ? ',' : '[' ) + serializedResponse ; if ( isCachedCommand ( cmd ) ) { cache += ( cache ? ',' : '[' ) + JSON . stringify ( serializedResponse ) ; } setImmediate ( runCommand , output , cache , cmdIndex + 1 ) ; } ) ; } // close the output JSON array output += ']' ; if ( cache === '' ) { cache = '[]' ; } else { cache += ']' ; } // turn results array into a reportable response (possibly gzipped) var options = { mimetype : CONTENT_TYPE_JSON } ; if ( ! shouldCompress ( output , acceptedEncodings ) ) { return respond ( options , output , cache ) ; } return compress ( output , function ( error , compressed ) { if ( error ) { // error during compression, send the original respond ( options , output , cache ) ; } else { options . encoding = 'gzip' ; respond ( options , compressed , cache ) ; } } ) ; }
recursively runs each command, until done, then calls respond
recursively runs each command until done then calls respond