I have a function making a callback to other functions in the class in linear fashion.
async runTask() {
debug('runTask');
debug(this.taskData);
try {
// Short circuit for testing
if (process.env.TASKTIMER) {
return setTimeout(() => {
this.taskComplete();
}, process.env.TASKTIMER);
}
// Setup project folder & Git Clone
await this.pullPackageWait();
// Create input and output directory
await this.mkIODirWait();
// Store task data as json
await this.writeTaskDataJSONWait();
// Download Inputs From Amazon
await this.downloadInputsWait();
// Link Input files
await this.linkInputsWait();
// Download Resources From Amazon
await this.downloadResourcesWait();
// Relax permission on tmp directory
await this.chmodTaskDir();
// Destroys the job after timeout
await this.jobTimeout();
// Determine Handler
console.log(this.taskData.handler);
await this.initHandler();
await this._handler.startJob();
} catch (err) {
this.logger.error(err);
return this.taskError();
}
I'm trying to convert the function calls into an array of functions following the this answer. After refactoring my functions looks as follows:
async runTask() {
debug('runTask');
debug(this.taskData);
try {
// Short circuit for testing
if (process.env.TASKTIMER) {
return setTimeout(() => {
this.taskComplete();
}, process.env.TASKTIMER);
}
let currentFunction = 0;
const functionsToRun = [
this.pullPackageWait,
this.mkIODirWait,
this.writeTaskDataJSONWait,
this.downloadInputsWait,
this.linkInputsWait,
this.downloadResourcesWait,
this.chmodTaskDir,
this.jobTimeout,
];
debug('Before The Loop');
for (currentFunction; currentFunction < functionsToRun.length; currentFunction++) {
debug('In The Loop');
await functionsToRun[currentFunction]();
console.log('Writing to the file');
await this.writeState();
}
// Determine Handler
console.log(this.taskData.handler);
await this.initHandler();
await this._handler.startJob();
} catch (err) {
this.logger.error(err);
return this.taskError();
}
With the original code the entire program runs normally, but after conversion it seems to break at the first callback. Did I make a mistake somewhere in conversion?