Search code examples
javascriptnode.jsnode-async

Utilizing Async NPM Module


So... I recently came across this node module: async. I just need a little "show and tell" or a Best Practice approach for the situation I have below. Below you can see my function GetBearerToken which takes an argument {bearer_token:token} without an issue.

My issue is the variable ss. I want to use it outside of this function and pass it to another function to do something. Of course when I try and access ss, it's undefined. I have tried some of the ways of making this work as indicated in the documentation, but I am apparently missing something. So any help would be great... Thanks

GetBearerToken({
      bearer_token: token
    }, function(error, result) {
      if (error) throw error;
      if (result) {
        var resultset
        var i;
        for (i = 0; i < result.length; i++) {
          resultset = (simpleObjectify(result[i].meta, result[i].rows))
        }

        var jstring = JSON.stringify(resultset);
        var obj = JSON.parse(jstring);
        var ss = obj[0].uuid;
        console.log(ss)
      })

Outside of function ss is undefined.


Solution

  • First read this

    What is the scope of variables in JavaScript?


    You can try using .waterfall method

    waterfall(tasks, [callback])

    Runs the tasks array of functions in series, each passing their results to the next in the array. However, if any of the tasks pass an error to their own callback, the next function is not executed, and the main callback is immediately called with the error.

    One example for what you are trying to accomplish

    async.waterfall([
          function(callback) {
    
            GetBearerToken({
                  bearer_token: token
                }, function(error, result) {
                  if (error) throw error;
                  if (result) { // *note* don't forget to handle the result properly if an error occurs or the result is not what you expected.
                    var resultset
                    var i;
                    for (i = 0; i < result.length; i++) {
                      resultset = (simpleObjectify(result[i].meta, result[i].rows))
                    }
    
                    var jstring = JSON.stringify(resultset);
                    var obj = JSON.parse(jstring);
                    var ss = obj[0].uuid;
                    callback(null, ss); // call the next callback in waterfall with ss value
                  }
                )
              },
              function(arg1, callback) {
                // arg1 now equals ss value 
                callback(null,'all completed');
              }
          ],
          function(err, result) {
            // result now equals 'all completed'
          });
    

    but since the code above seems already a step towards to wrong direction when it comes to debugging. But take a look at example of .waterfall who it breaks down the callbacks and then calls the .waterfall method.