Wish Async.Auto/Resumable step switch like JavaScript/Node?

Discussion in 'Bugs & wishlist' started by techknight, Jun 6, 2018.

  1. techknight

    techknight Well-Known Member Licensed User

    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:

    Code:
    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':'user@example.com'});
        }]
    }, function(err, results) {
        console.log('err = ', err);
        console.log('results = ', results);
    });
    any thoughts?
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    This is already very simple to implement with Wait For.

    Code:
    Wait For (FirstSub) Complete (Result As Boolean)
    If Result Then
     SecondSub
    If the code above is not clear then you should watch the resumable subs video tutorial.
     
    j_o_h_n likes this.
  3. techknight

    techknight Well-Known Member Licensed User

    Oh. I thought those were only particular to certain functions like MsgBoxAsync. I didnt know they could be used universally.
     
  4. j_o_h_n

    j_o_h_n Member Licensed User

    Resumable subs are amazing. It's worth watching that video several times
     
    Erel likes this.
  5. Erel

    Erel Administrator Staff Member Licensed User

  6. techknight

    techknight Well-Known Member Licensed User

    Yea, I cant watch videos here. Firewall.

    Personally, I dont like videos, I am old school.
     
  7. Erel

    Erel Administrator Staff Member Licensed User

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice