After get data in Storage, how can execute next function?


#1

I have already saved my data in Storage.

import { Storage } from '@ionic/storage';
    ... ... ...
this.storage.set('usertype', usertype);

I have to create a function to get data from storage.
And the program must execute next functions after get data from storage.
How can I create get function or build programming logic?


#2

setter and getter on storage return promises so you can do following

this.storage.get('name').then((name) => {
  console.log('Me: Hey, ' + name + '! You have a very nice name.');
  console.log('You: Thanks! I got it for my birthday.');
});

#3

Thank you @ankushagg93
I want to get data from storage as synchronous method.
You are right, the setter and getter on storage return promise, but I need function immediately to get data from storage.
Is it possible?


#4

No.

Restructure your code to only do what needs to be done once the promise resolves. Often, this involves assigning dummy default values to some object properties.


#5

Thank you @rapropos
Do you have any link for restructured?
I need more detail.


#6

@ankushagg93 already gave you a very clear example.