I try get value from ionic storage, but it's doesn't work here. Why GET2 execute before storage.get ? My brain is broken, help please.
public storageGet(key: string){
var uid = 0;
this.storage.get(key).then((val) => {
console.log('GET1: ' + key + ': ' + val);
if (val != null) { uid = val;}
});
console.log('GET2: ' + key + ': ' + uid);
return uid;
}
Return:
GET2: uid: 0
GET1: uid: 1
You need to understand how promise works.
This code is asynchronous, all lines in the then
callback will be execute, but you can't decide when.
The console.log("GET2")
is executed strictly after the storage.get
, this part is synchronous.