ITPub博客

首页 > 移动开发 > ANDROID > 教你实现快应用storage接口同步调用

教你实现快应用storage接口同步调用

原创 ANDROID 作者:华为开发者论坛 时间:2021-06-24 14:18:34 0 删除 编辑

快应用异步接口支持返回 Promise(1010+)的方式 ,开发者配套 async和await的方式编写代码,达到同步效果。对于接口调用成功是返回一个对象res = {data} ,开发者可以通过res.data获取接口实际返回的结果,通过res.code获取失败的返回code。

以 storage.get()接口为例,代码如下:

<script>
  import storage from '@system.storage';
  const injectRef = Object.getPrototypeOf(global) || global;
  // 注入regeneratorRuntime
  injectRef.regeneratorRuntime = require('@babel/runtime/regenerator');
 
  module.exports = {
    onDestroy: function () {
      console.info("onDestroy");
    },
    getValue:  async function () {
      try {
        let re = await storage.get({
        key: 'name'
      });
      console.info("getValue re="+JSON.stringify(re));
      let value=re.data;
      } catch (error) {
          console.info("getValue error="+error);
      }
    }
  }
</script>

输出如下:

getValue re={"data":"hanmeimei"}

注意事项:

对于 await的方式调用需要引入@babel/runtime/regenerator。

 

欲了解更多详情,请参见:

快应用回调介绍:

https://developer.huawei.com/consumer/cn/doc/development/quickApp-References/quickapp-api-rules#h1-1575622944242


原文链接: https://developer.huawei.com/consumer/cn/forum/topic/0204418582525080548?fid=18

原作者:Mayism

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69970551/viewspace-2778136/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2020-04-21

  • 博文量
    402
  • 访问量
    726967