离子等待本地存储

时间:2018-12-24 15:55:56

标签: ionic-framework async-await local-storage

在继续执行我的功能之前,我需要等待localStorage检索值。我读到我必须使用“异步/等待”指令,但是我发现的所有示例都没有真正起作用。

我的代码是

  _ajustes.EsPrimeraVez().then (async (promAjuste)=> {
    await console.log('TEST! en el then');
  });
  console.log('TEST! Post');

并在“调整”中:

public EsPrimeraVez() {
   console.log('TEST! entrando en primeravez');
   return this.storage.get('primeravez');

但是它不等待结果继续...这可能吗? 谢谢大家新年快乐!

1 个答案:

答案 0 :(得分:1)

如果要等待从函数中检索值,为什么要使用prmoise / then syntac。下面的代码将顺序运行,一旦执行函数,它将为变量“ mystoragevalue”分配本地存储值。

var mystoragevalue = _ajustes.EsPrimeraVez();
console.log('TEST! Post');

并在“调整”中:

public EsPrimeraVez() {
   console.log('TEST! entrando en primeravez');
   return this.storage.get('primeravez');
}

希望这有助于...编码愉快... !!!