RC1 How to get item from ionic Storage or LocalForage in synchronous process

Hi all,

I’m using ionic Storage and LocalForage but had problems when get item on both. Get item in Storage or LocalForage running like async process.

How to get item using ionic storage and LocalForage running with sync process?

This my code:

import { Injectable } from ‘@angular/core’;
import { Http, Headers } from ‘@angular/http’;
import { LoadingController } from ‘ionic-angular’;
import { Storage } from ‘@ionic/storage’;
import { JwtHelper, tokenNotExpired } from ‘angular2-jwt’;
import ‘rxjs/add/operator/map’;
import mylocalforage from “localforage”;

@Injectable()
export class PhotoPostingService {

POSTING_URL: string = "http://xxxxxxx";
contentHeader: Headers = new Headers({"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"});

jwtHelper: JwtHelper = new JwtHelper();
user: any;
error: string;
local: Storage;
token: any;
tokenf: any;
data: any;

constructor(private http: Http, local: Storage, public loadingCtrl: LoadingController) {
this.local = local;
}

load() {
mylocalforage.getItem(‘tokenf’).then(tokenf => {
console.log(‘Token forage start’);
this.tokenf = tokenf;
console.log('Token forage, ’ + this.tokenf + ‘! You have a very nice token.’);
});

  this.local.get('token').then(token => {
  	console.log('Token storage start');
  	this.token = token;
  	console.log('Token, ' + this.token + '! You have a very nice token.');
  });
  
  if (this.data) {
  	return Promise.resolve(this.data);
  }
  console.log('token posting = ' + this.token);
  let postingUrl : string = this.POSTING_URL+'?token='+this.tokenf;
  
  return new Promise(resolve => {
  	this.http.get(postingUrl,  { headers: this.contentHeader })
  	  .map(res => res.json())
  	  .subscribe(data => {
  		this.data = data;
  		resolve(this.data);
  	  });
  	}
  );

}

}

Thanks and regards,
Suwardi

You should chain the promises:

The result code is something like that:

        return mylocalforage.getItem('tokenf').then(tokenf => {
            console.log('Token forage start');
            this.tokenf = tokenf;            
            console.log('Token forage, ' + this.tokenf + '! You have a very nice token.');
            return tokenf;
        }).then(() => {
            return this.local.get('token').then(token => {
                console.log('Token storage start');
                this.token = token;                
                console.log('Token, ' + this.token + '! You have a very nice token.');
                return token;
            });
        }).then(() => {
            if (this.data) {
                return Promise.resolve(this.data);
            }
            console.log('token posting = ' + this.token);
            let postingUrl: string = this.POSTING_URL + '?token=' + this.tokenf;

            return new Promise(resolve => {
                this.http.get(postingUrl, { headers: this.contentHeader })
                    .map(res => res.json())
                    .subscribe(data => {
                        this.data = data;
                        resolve(this.data);
                    });
            }
            );
        });