0

Here is my code...

'use strict';
var React = require('react-native');
var {
  AsyncStorage
} = React;



exports.buildUrl = function(){
  var test = new Sales();
  test.getOrder();
}

class queryBuilder{

  constructor() {

  }

  getUrl(){
    AsyncStorage.getItem('auth').then((value) => {
      var auth = JSON.parse(value);
      var store = auth.url;
      console.log('store = ' + store);
      return store;
    });
  }

}

class Sales extends queryBuilder{

  async getOrder(){
    console.log('get order hit');
    try{
        var urlstart =  await this.getUrl();
    } catch(e) {
      console.log('ERROR')
      console.log(e);
    }
    console.log('URL BELOW');
    console.log(urlstart);
    console.log('URL ABOVE');
  }

}

In my getOrder method I was hoping that urlstart would not be undefined because i have used await.

The console.log within getUrl() method is the last thing to return in my console.

Maybe it's that saturday feeling, any help much appreciated!

Matt The Ninja
  • 2,641
  • 4
  • 28
  • 58
  • Why not make it a simple `async getUrl() { return JSON.parse(await AsyncStorage.getItem('auth')).url; }`? – Bergi May 07 '16 at 21:20

1 Answers1

0

Resovled this myself. Async Await require a promise to be returned on the function i'm calling. I have modified the getUrl() method like below..

  getUrl(){
    return new Promise(function(resolve, reject) {
      AsyncStorage.getItem('auth').then((value) => {
        var auth = JSON.parse(value);
        var store = auth.url;
        console.log('store = ' + store);
        resolve(store);
      });
    }

Any further sugestions still greatly appreciated.

Updated better, Thanks to Yuri

getUrl(){ return AsyncStorage.getItem('auth').then(value => JSON.parse(value).url) }
Matt The Ninja
  • 2,641
  • 4
  • 28
  • 58