1

Hello im new in typeScript and Angular. I need to get a value in storage, the storage.get return a promise, so i try:

getNomeUsuario():string{
    var that = this;
    this.storage.get('nomeUsuario').then(function(nome){
        that.nomeUsuario = nome;
    }.bind(this));
    return this.nomeUsuario;
}

The problem is that always return a undefined. I also get the same with an arrow function.

getNomeUsuario():string{
    this.storage.get('nomeUsuario').then((nome)=>{
        this.nomeUsuario = nome;
    });
    return this.nomeUsuario;
}

How can I return the nome value ?

Sorry about my English hehe

jeanfrg
  • 2,366
  • 2
  • 29
  • 40
Rodrigo Sene
  • 309
  • 1
  • 13
  • It's because `getNomeUsuario()` almost instantly returns the current value of `this.nomeUsuario`, before asynchronous `this.storage.get()` changes the `this.nomeUsuario` value. – kamyl Jul 29 '17 at 21:00

2 Answers2

1

Since this.storage.get("nomeUsuario") is asynchronous, there's no way the getNomeUsuario() method can return it's value without returning a Promise, so you'll need to do it like this:

getNomeUsuario(): Promise<string> {
  return this.storage.get("nomeUsuario");   
}

And handle the Promise where you call getNomeUsuario().

ps: you could also use an Observable, but it would add complexity without any benefits.

Lucas Moulin
  • 2,450
  • 3
  • 32
  • 41
0

this.storage.get('nomeUsuario') executes asynchronously, and when you returns that.nomeUsuario, the promise wasn't executed yet and that.nomeUsuario still undefined. Try to insert this return into .then section like this:

getNomeUsuario():string{
    var that = this;
    this.storage.get('nomeUsuario').then((nome)=>{
        that.nomeUsuario = nome;
        return that.nomeUsuario;
    });
}
P.S.
  • 15,970
  • 14
  • 62
  • 86
  • when i try return inside promise i get a compilation error, getNomeUsuario need to return a string; – Rodrigo Sene Jul 29 '17 at 21:25
  • if i change return to any getNomeUsuario():any its compile but the value return is undefined. – Rodrigo Sene Jul 29 '17 at 21:26
  • Hm, try to return `this.storage.get(...` too, like this: `return this.storage.get(...` – P.S. Jul 29 '17 at 21:27
  • i get a promisse return not a string. – Rodrigo Sene Jul 29 '17 at 21:31
  • @RodrigoSene, yes, it was a bad idea. Try to `console.log(that.nomeUsuario)` after `that.nomeUsuario = nome;` row. what's the result? – P.S. Jul 29 '17 at 21:33
  • this.storage.get('nomeUsuario').then((nome)=>{ that.nomeUsuario = nome; console.log(that.nomeUsuario); return that.nomeUsuario; }); whit this i get the name... console.log(JSON.stringify(this.dadosBaseProvider.getNomeUsuario())); with this i get undefined – Rodrigo Sene Jul 29 '17 at 21:40
  • @RodrigoSene, and what do you get in console, when you are trying to `console.log(that.nomeUsuario);`? – P.S. Jul 29 '17 at 21:42
  • the name; 2VM11199 main.js:1139 Rodrigo Sene – Rodrigo Sene Jul 29 '17 at 21:47
  • @RodrigoSene, is it an expected result? what it should be? – P.S. Jul 29 '17 at 21:48
  • yes, inside the arrow function the result is exactly the expected. – Rodrigo Sene Jul 29 '17 at 21:50
  • @RodrigoSene, sorry, but it looks like i don't have enough knowledge to advice something else =( – P.S. Jul 29 '17 at 21:52
  • thanks for help... i solve this with [link](https://stackoverflow.com/questions/38291783/how-to-return-value-from-function-which-has-observable-subscription-inside) – Rodrigo Sene Jul 29 '17 at 21:57
  • @RodrigoSene, hey, it was an `Observable`! Yes, we should work with `Observables` and `Promises` different ways. You wrote "the storage.get return a promise", so i thought that it is promise... Nice to hear, that you founded a solution! – P.S. Jul 29 '17 at 22:00
  • the return of storage.get is a promise... so .... return Observable.fromPromise(this.storage.get('nomeUsuario')); i think is not the best way.. but =( – Rodrigo Sene Jul 29 '17 at 22:05