0

Im using npm plaid and I want to be able to have access to the variable trans outside this plaidClient.getTransactions. Thanks

var trans;
 var startDate = moment().subtract(30, 'days').format('YYYY-MM-DD');
 var endDate = moment().format('YYYY-MM-DD');
 plaidClient.getTransactions(AccessToken, startDate, endDate, {
   count: 250,
   offset: 0,
 }, function(error, transactionsResponse) {
   if (error != null) {
    console.log(JSON.stringify(error));
    return response.json({
    error: error
    }); 
  }
  var trans = transactionsResponse
});

var trans //should work here
David Ramirez
  • 207
  • 5
  • 18
  • This might help you [link](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Hassan Imam Aug 14 '17 at 18:25
  • 1
    *go to the shop and get me an icecream. give me the icecream now.* => you get an empty icecream :/ – Jonas Wilms Aug 14 '17 at 18:26

3 Answers3

0

You have a fundamental misunderstanding of how asynchronous Javascript works.

I suggest you read one of many tutorials on asychronous flow of execution in Javascript to understand why this question is asking the wrong thing.

A quick google search suggests a number of articles on this topic.

Ex https://www.pluralsight.com/guides/front-end-javascript/introduction-to-asynchronous-javascript

Deadron
  • 5,135
  • 1
  • 16
  • 27
0

You cannot do this with asynchronous code, let alone synchronous code. You will never have access to the variable trans outside of the scope of the function it is defined in.

If you want to have access to the value that trans represents outside of the function trans is defined in, you need some kind of events or streaming.

Try RxJS.

With RxJS you can do the following (not tested):

 var trans = Rx.Subject();

 var startDate = moment().subtract(30, 'days').format('YYYY-MM-DD');
 var endDate = moment().format('YYYY-MM-DD');
 plaidClient.getTransactions(AccessToken, startDate, endDate, {
   count: 250,
   offset: 0,
 }, function(error, transactionsResponse) {
   if (error != null) {
    console.log(JSON.stringify(error));
    return response.json({
    error: error
    }); 
  }

  // Pass the value into the stream.
  trans.next(transactionsResponse);
});

// Every time the `trans.next(...)` line is called in the HTTP call,
// this function will be invoked.
trans.asObservable().subscribe(function (transactionsResponse) {
  // use "transactionsResponse" how you please...
});

What we do above is create a subject, called trans. When your HTTP response comes back, we add the value to the trans subject stream. And at the bottom, we subscribe to that stream, and any time something is added to it the callback will be invoked. This way you always have the "access" you are looking for to trans, even with asynchronous code.

Lansana Camara
  • 9,497
  • 11
  • 47
  • 87
  • using another lib makes the whole thing not easier. use promises or async / await – Jonas Wilms Aug 14 '17 at 18:33
  • True, but he is already using Vanilla JS which implies promises/async/await will not be built in (or at least widely supported) for him by default. That means he'd have to add polyfills for those as well and/or learn how to use them, which wouldn't be too much different than taking an RxJS approach. – Lansana Camara Aug 14 '17 at 18:37
0

You should define a global variable, i.e. app and use it to keep your needed access variables. Something just like that:

app = {};
//....

var startDate = moment().subtract(30, 'days').format('YYYY-MM-DD');
var endDate = moment().format('YYYY-MM-DD');

plaidClient.getTransactions(AccessToken, startDate, endDate, {
       count: 250,
       offset: 0
}, function(error, transactionsResponse) {
       if (error != null) {
          console.log(JSON.stringify(error));
          return response.json({
              error: error
          }); 
       }

       app.trans = transactionsResponse;
});

//....
console.log(app.trans); // should print correctly
pitter
  • 33
  • 3