2

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.

Seth
  • 10,198
  • 10
  • 45
  • 68

1 Answers1

1

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.

Community
  • 1
  • 1
Gntem
  • 6,949
  • 2
  • 35
  • 48
  • Thank you GeoPhoenix for your suggestions. I went ahead and cleaned up the code and took into account your suggestions and example. One final question and I will consider this answered. If I wanted to wrap this whole thing into a greater function and then get the output of the async functionality, say output 1 for complete or 0 for error, what would the best approach be? – Matthew Haines Apr 22 '16 at 12:42
  • "the best approach" is the one that you understand better and its simpler to be implement. Essentially i would create a waterfall within an waterfall if i were only using `async` but this approach would be complicated for most of the users. I would consider using `events` or promises and structure properly my code be as maintainable and simpler as possible. – Gntem Apr 22 '16 at 13:21
  • Thank you GeoPhoenix. You are correct, I need to wrap my head around the async concepts a little better. With "events" are you talking about Event Emitter in the EventEmitter Class? Thanks again. – Matthew Haines Apr 22 '16 at 13:37
  • Yes those are the basic classes i'm sure you can find many modules around who make working with events easier, you don't need to reinvent things. – Gntem Apr 22 '16 at 13:39
  • Thank you again GeoPhoenix,I believe you have help me out tremendously. – Matthew Haines Apr 22 '16 at 14:11