0

Im having a problem with what I believe is a javascript promise issue. Although this is an angular6 app, I think that anyone with JavaScript expertise could probably help me out here.

Im calling a simple js function in a separate js file from my angular component. The simple js function grabs a list of connection strings from a local database and tries to return said list. I set an alert as you can see in my code so i know im getting the information that i need.

The problem is that by the time It gets back to the top calling method (line 4), its empty! Its as if its returning a promise instead of the actual array.. How can I get the actual value back?

Thanks in advance.

 //MY ANGULAR6 COMPONENT
   public getLocal() {
    this.Connections = [];
    this.Connections = fetchFromLocalDatabase();
   }

//MY IMPORTED JS FILE
fetchFromLocalDatabase() {
    let myArray = [];
    idb.open('InfoRail', 1).then(function (db) {
      var tx = db.transaction(['ConnectionStrings'], 'readonly');
      var store = tx.objectStore('ConnectionStrings');
      return store.getAll();
    }).then(function (items) {
       myArray = items;
       alert('show me ' + myArray[0].DatabaseName);      
    });
    return myArray;
  }
  
 
Rick Pace
  • 11
  • 2
  • actually, `fetchFromLocalDatabase` will be returning an empty array - which at "some time in the future"™ would be filled with `items`, except that it won't because the common reference is broken by `myArray = items` - so I think you not only have an issue with asynchrony, you seem to also misunderstand what you are seeing .. an empty array is not a promise, it is an empty array – Jaromanda X Aug 09 '18 at 02:40
  • Thanks for replying but ive tried simply returning 'items' (which is an array) without assigning to myArray and dozens of other variations before posting on here. My question is, how can i wait for this "some time in the future" before the call makes a round trip? – Rick Pace Aug 09 '18 at 02:57
  • see duplicate for an explanation of what you need to do – Jaromanda X Aug 09 '18 at 02:58

0 Answers0