# How to get the LocalStorage value out of promise scope ionic 4

**URL:** <https://forum.ionicframework.com/t/how-to-get-the-localstorage-value-out-of-promise-scope-ionic-4/161492>\
**Category:** ionic-v3\
**Created:** [April 12, 2019, 2:16pm UTC](https://forum.ionicframework.com/t/how-to-get-the-localstorage-value-out-of-promise-scope-ionic-4/161492 "2019-04-12T14:16:32Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![gowtham78](https://avatars.discourse-cdn.com/v4/letter/g/57b2e6/32.png) [@gowtham78](https://forum.ionicframework.com/u/gowtham78)\
**Post date:** [April 12, 2019, 2:16pm UTC](https://forum.ionicframework.com/t/how-to-get-the-localstorage-value-out-of-promise-scope-ionic-4/161492/1 "2019-04-12T14:16:32Z")

</div>

So can you please correct me if i am wrong …

//Its working fine  
this.storage.get(‘user\_id’).then((result) =\> {  
console.log(result);  
});  
//its not working  
this.storage.get(‘user\_id’).then((result) =\> {

```
});

```

console.log(result);

---

<div class="post-metadata">

**Author:** ![tumain](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/tumain/32/107546_2.png) [@tumain](https://forum.ionicframework.com/u/tumain)\
**Post date:** [April 12, 2019, 2:27pm UTC](https://forum.ionicframework.com/t/how-to-get-the-localstorage-value-out-of-promise-scope-ionic-4/161492/2 "2019-04-12T14:27:19Z")

</div>

Not sure what you’re asking here. Looks like there could be a syntax error?

Your console.log line is not within the promise callback, so it wouldn’t work in the second scenario.

---

<div class="post-metadata">

**Author:** ![gowtham78](https://avatars.discourse-cdn.com/v4/letter/g/57b2e6/32.png) [@gowtham78](https://forum.ionicframework.com/u/gowtham78)\
**Post date:** [April 12, 2019, 2:48pm UTC](https://forum.ionicframework.com/t/how-to-get-the-localstorage-value-out-of-promise-scope-ionic-4/161492/3 "2019-04-12T14:48:28Z")

</div>

Thanks for your reply @tumain this is my actual code  
getstorage()  
{  
this.storage.get(‘user\_id’).then((result) =\> {  
console.log(result);  
});  
}

let value =this.getstorage();

//then my output is like this

1. ZoneAwarePromise {\_\_zone\_symbol\_\_state: null, \_\_zone\_symbol\_\_value: Array(0)}

2. \_\_zone\_symbol\_\_state: true

3. \_\_zone\_symbol\_\_value: “5”

4. **proto** : Object

then how can i get this value \_\_zone\_symbol\_\_value: “5”

---

<div class="post-metadata">

**Author:** ![jjdev](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@jjdev](https://forum.ionicframework.com/u/jjdev)\
**Post date:** [April 12, 2019, 2:58pm UTC](https://forum.ionicframework.com/t/how-to-get-the-localstorage-value-out-of-promise-scope-ionic-4/161492/4 "2019-04-12T14:58:31Z")

</div>

i prefer the async/await syntax for promises, it feels easier to read  
the method needs to `return` some value if you are assigning it to a variable

```auto
async getStorage(): Proimise<any> {
    try {
        const result = await this.storage.get('user_id');
        console.log(result);
        return result;
    }
    catch(e) { console.log(e) }
}

let value = this.getStorage();

```

alternatively, without async:

```auto
getStorage() {
    return this.storage.get('user_id').then((result) => {
        console.log(result);
        return result;
    });
}

let value = this.getStorage();

```

do either of these work?

---

<div class="post-metadata">

**Author:** ![gowtham78](https://avatars.discourse-cdn.com/v4/letter/g/57b2e6/32.png) [@gowtham78](https://forum.ionicframework.com/u/gowtham78)\
**Post date:** [April 12, 2019, 3:03pm UTC](https://forum.ionicframework.com/t/how-to-get-the-localstorage-value-out-of-promise-scope-ionic-4/161492/5 "2019-04-12T15:03:36Z")

</div>

Thanks @jjdev Checked  
Nope!  
same output i got

ZoneAwarePromise {\_\_zone\_symbol\_\_state: null, \_\_zone\_symbol\_\_value: Array(0)}

\_\_zone\_symbol\_\_state: true  
\_\_zone\_symbol\_\_value: “5”  
**proto** : Object

---

<div class="post-metadata">

**Author:** ![jjdev](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@jjdev](https://forum.ionicframework.com/u/jjdev)\
**Post date:** [April 12, 2019, 4:14pm UTC](https://forum.ionicframework.com/t/how-to-get-the-localstorage-value-out-of-promise-scope-ionic-4/161492/6 "2019-04-12T16:14:23Z")

</div>

```auto
let value: any;

getStorage() {
    try { return await this.storage.get('user_id') }
    catch(e) { console.log(e) }
}

this.getStorage().then((result) => {
    if (result) {
        console.log(result);
        value = result;
    }
});

```

Here I have just returned the promise entirely, and handle the ‘then’ outside of the method.

I’m not sure why this isn’t working for you, I haven’t had many problems using localStorage this way

---

<div class="post-metadata">

**Author:** ![gowtham78](https://avatars.discourse-cdn.com/v4/letter/g/57b2e6/32.png) [@gowtham78](https://forum.ionicframework.com/u/gowtham78)\
**Post date:** [April 15, 2019, 5:07am UTC](https://forum.ionicframework.com/t/how-to-get-the-localstorage-value-out-of-promise-scope-ionic-4/161492/7 "2019-04-15T05:07:46Z")

</div>

Thanks @jjdev I tried all your ways but only got this output

ZoneAwarePromise {\_\_zone\_symbol\_\_state: null, \_\_zone\_symbol\_\_value: Array(0)}

\_\_zone\_symbol\_\_state: true  
\_\_zone\_symbol\_\_value: “5”  
**proto** : Object

---

<div class="post-metadata">

**Author:** ![gowtham78](https://avatars.discourse-cdn.com/v4/letter/g/57b2e6/32.png) [@gowtham78](https://forum.ionicframework.com/u/gowtham78)\
**Post date:** [April 15, 2019, 5:39am UTC](https://forum.ionicframework.com/t/how-to-get-the-localstorage-value-out-of-promise-scope-ionic-4/161492/8 "2019-04-15T05:39:32Z")

</div>

Anyone having a idea ???

---

<div class="post-metadata">

**Author:** ![shyamsingh](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@shyamsingh](https://forum.ionicframework.com/u/shyamsingh)\
**Post date:** [April 15, 2019, 5:57am UTC](https://forum.ionicframework.com/t/how-to-get-the-localstorage-value-out-of-promise-scope-ionic-4/161492/9 "2019-04-15T05:57:16Z")

</div>

Please try this,

let x = storage.get(‘user\_id’)

---

<div class="post-metadata">

**Author:** ![jjdev](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@jjdev](https://forum.ionicframework.com/u/jjdev)\
**Post date:** [April 15, 2019, 12:47pm UTC](https://forum.ionicframework.com/t/how-to-get-the-localstorage-value-out-of-promise-scope-ionic-4/161492/10 "2019-04-15T12:47:16Z")

</div>

I would just double check here and make sure you have everything implemented properly

[Storage docs](https://ionicframework.com/docs/building/storage)

---

<div class="post-metadata">

**Author:** ![belapyc](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@belapyc](https://forum.ionicframework.com/u/belapyc)\
**Post date:** [April 16, 2019, 3:05pm UTC](https://forum.ionicframework.com/t/how-to-get-the-localstorage-value-out-of-promise-scope-ionic-4/161492/11 "2019-04-16T15:05:29Z")

</div>

Try this.

```auto
var result: any;

this.storage.get(‘user_id’).then((data) => {
 result = data
});
console.log(result);

```

---

<div class="post-metadata">

**Author:** ![aabbcc1241](https://avatars.discourse-cdn.com/v4/letter/a/c6cbf5/32.png) [@aabbcc1241](https://forum.ionicframework.com/u/aabbcc1241)\
**Post date:** [April 27, 2019, 7:13am UTC](https://forum.ionicframework.com/t/how-to-get-the-localstorage-value-out-of-promise-scope-ionic-4/161492/12 "2019-04-27T07:13:19Z")

</div>

you do not use a promise value directly.  
you either use it in it’s then/catch method with callback,  
or use it with await inside async block.

you may learn more about async programming in javascript from this youtube video:

---

<div class="post-metadata">

**Author:** ![jcortesjara](https://avatars.discourse-cdn.com/v4/letter/j/9fc348/32.png) [@jcortesjara](https://forum.ionicframework.com/u/jcortesjara)\
**Post date:** [November 14, 2019, 2:20pm UTC](https://forum.ionicframework.com/t/how-to-get-the-localstorage-value-out-of-promise-scope-ionic-4/161492/13 "2019-11-14T14:20:29Z")

</div>

Could you find any answer? I have same issue, but only when I build the app with capacitor, on the browser works fine

---

<div class="post-metadata">

**Author:** ![alitalaee](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@alitalaee](https://forum.ionicframework.com/u/alitalaee)\
**Post date:** [February 10, 2020, 2:05pm UTC](https://forum.ionicframework.com/t/how-to-get-the-localstorage-value-out-of-promise-scope-ionic-4/161492/14 "2020-02-10T14:05:23Z")

</div>

yes  
it is really easy for start in you`re service create a function and then paste below text on that. for example i have a method to show PROFILE info in USER-STORAGE service

//read user to show in profile pages  
//Iuser this my interface you can see on below  
// this method return Promise  
read\_to\_show\_profile(): Promise {  
return this.storage.get(user\_storage\_DB);  
}

/**/  
export interface Iuser {  
id: string;  
name: string;  
mobileNumber: number;  
city: string;  
subCity: string;  
galleryName: string;  
imageSrc: string;  
}  
/**\*/

and then in you`re component u can import SERVICE and use it like below  
MY.COMPONENT.TS file

public current\_usr: Iuser = {};

constructor(  
private storage\_service: StorageUserService  
) {}

ngOnInit() {  
//read data from local storage and assign it to CURRENT\_USER  
this.storage\_service.read\_to\_show\_profile().then(result =\> {  
this.current\_usr = result;  
console.log(this.current\_usr);  
});
