There is a module for NodeJS/JavaScript called Async, and there is a function called Auto. It works along the lines of resumablesubs/Callback so its right up the alley of the current line in development.
So you have a Step 1, Step 2, Step 3, Step 4, etc... But Step 2 cannot execute until Step 1 is finished. And then Step 2 has a condition flag check set by Step 1 so if Step 1 has an invalid result, or doesn't run, again, Step 2 doesn't run. But, if there is no condition checks in Step 1 and 2, then they run simultaneously.
The part that I want is this: So Step 1 runs, the program then goes and does its thing, and then the Sub resumes to run Step 2 after Step 1 has completed.
This would help me with severely nested If/Then statements. If, then If then If then.... you know.
Here is an example of Async.Auto:
any thoughts?
So you have a Step 1, Step 2, Step 3, Step 4, etc... But Step 2 cannot execute until Step 1 is finished. And then Step 2 has a condition flag check set by Step 1 so if Step 1 has an invalid result, or doesn't run, again, Step 2 doesn't run. But, if there is no condition checks in Step 1 and 2, then they run simultaneously.
The part that I want is this: So Step 1 runs, the program then goes and does its thing, and then the Sub resumes to run Step 2 after Step 1 has completed.
This would help me with severely nested If/Then statements. If, then If then If then.... you know.
Here is an example of Async.Auto:
B4X:
async.auto({
// this function will just be passed a callback
readData: async.apply(fs.readFile, 'data.txt', 'utf-8'),
showData: ['readData', function(results, cb) {
// results.readData is the file's contents
// ...
}]
}, callback);
async.auto({
get_data: function(callback) {
console.log('in get_data');
// async code to get some data
callback(null, 'data', 'converted to array');
},
make_folder: function(callback) {
console.log('in make_folder');
// async code to create a directory to store a file in
// this is run at the same time as getting the data
callback(null, 'folder');
},
write_file: ['get_data', 'make_folder', function(results, callback) {
console.log('in write_file', JSON.stringify(results));
// once there is some data and the directory exists,
// write the data to a file in the directory
callback(null, 'filename');
}],
email_link: ['write_file', function(results, callback) {
console.log('in email_link', JSON.stringify(results));
// once the file is written let's email a link to it...
// results.write_file contains the filename returned by write_file.
callback(null, {'file':results.write_file, 'email':'[email protected]'});
}]
}, function(err, results) {
console.log('err = ', err);
console.log('results = ', results);
});
any thoughts?