Storage data not assign to variable (RC0)

In using Storage, when I wanna get a value, for example by below codes:

 this.storageCtrl.get("storedValue").then(data => {
       this.variable = data;
       console.log(data);
 });

The stored value log in console but not assigned to my variable and when I use the variable in an other place I face with undefined error.

What should I do?

I really need help! :confused:

Thanks,
But I have the same problem with Http request. Is it possible to use callback with Http too?
Could you give me an example of callback?

I think both setTimeout and the callback hackery are major antipatterns here that will result in unreliable unmaintainable spaghetti.

If a service returns a Promise, as the storage service here apparently does, the underlying returned value is ONLY available in a then clause. You must design the rest of the application to be able to deal with that. If it returns an Observable, like Http does, you must do similarly with subscriptions and operators like map.

Promises and Observables were specifically designed to let people stop dealing with callback hell and avoid race conditions implicit with timeouts. Yes, there’s a learning curve involved, but it will really pay off if you take the time to wrap your head around how these constructs are intended to be used.

1 Like

Thank you very much for this detailed explanation.
Yes, I solved that issue by the way that you said, It was very confusing but finally I did!