简体   繁体   English

离子存储 GET

[英]Ionic storage GET

I try get value from ionic storage, but it's doesn't work here.我尝试从离子存储中获取价值,但在这里不起作用。 Why GET2 execute before storage.get?为什么 GET2 在 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.您需要了解 promise 的工作原理。

This code is asynchronous, all lines in the then callback will be execute, but you can't decide when.这段代码是异步的, then回调中的所有行都会被执行,但你无法决定何时执行。

The console.log("GET2") is executed strictly after the storage.get , this part is synchronous. console.log("GET2")严格在storage.get之后执行,这部分是同步的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM