I'm trying to achieve the following:
Please consider running this in jasmine test framework that does not support async/await yet
async.waterfall calls a function that has async.each to trigger the creation of schemas and tables. The steps in async waterfall have to be executed sequentially i.e., schemas have to be created before creating tables. The problem I'm facing is that the first call to create schemas is executed but the callback is never returned to the async.waterfall. So, the next step in async.waterfall is never executed.
Timeline or Flow:
driverFunction (async.waterfall) invokes the createFunction.
The createFunction (asyncCreateSchema etc.,) function invokes the doSomething for each file in the array.
doSomething executes a jar file and returns a success or an error.
Here's my code:
'use strict'
let async = require('async');
function doSomething(file, done) {
console.log(file);
return done(null, true);
}
function asyncCreateSchema(files, done) {
async.each(
files,
function(file, callback) {
if (file.startsWith('schema')) {
doSomething(file, callback);
}
else{
callback();
}
},
function(err) {
if (err) {
console.log(err);
}
console.log('create schema done');
});
}
function asyncCreateTables(files, done) {
async.each(
files,
function(file, callback) {
if (file.startsWith('table')) {
doSomething(file, callback);
}
else{
callback();
}
},
function(err) {
if (err) {
console.log(err);
}
console.log('create schema done');
});
}
var files = ['schema.json', 'schema_1.json', 'table.json'];
async.waterfall([
next => asyncCreateSchema(files, next),
(nil, next) => asyncCreateTables(files, next),
],
function(err, res) {
if (err) {
throw new Error("Setup error: " + err.message);
} else {
console.log(res);
}
}
);
What am I doing wrong here? Please explain the flow of callback functions in this scenario using the async npm package.