简体   繁体   English

异步等待离子存储获取

[英]async await ionic Storage get

I thought to be smart creating a little snippet code that can both fetch and set some data to the local storage with Ionic and Storage. 我以为创建一些片段代码可以很聪明,该代码片段可以使用Ionic和Storage来获取和设置一些数据到本地存储。 My helper function looks like: 我的辅助函数如下所示:

async local(key, value?:any) {
    if(value === undefined) {
        return await this.storage.get(key);
    }

    return this.storage.set(key, value);
}

But when I call it from another typescript file like let var = this.helperProvider.local('myTestVar'); 但是当我从另一个打字稿文件中调用它时,比如let var = this.helperProvider.local('myTestVar'); I get a 'magic' object as a response: 我得到一个“魔术”对象作为回应:

t {__zone_symbol__state: null, __zone_symbol__value: Array(0)}
__zone_symbol__state
:
true
__zone_symbol__value
:
null
__proto__
:
Object

Is the above possible so my local() method just returns the value in the local storage? 以上可能吗,所以我的local()方法只返回本地存储中的值?

The magic object is also a promise you need to await . 魔术对象也是您需要await的承诺。 Try calling it in another async function: 尝试在另一个async函数中调用它:

(async function() {
    let val = await this.helperProvider.local('myTestVar');
    console.log(val);
})()

Or use then 或者用then

this.helperProvider.local('myTestVar').then(val => console.log(val));

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

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