The Fast App asynchronous interface supports returning a Promise (1010+). Developers can write code in sync with async and await. If the successful interface call returns an object res = {data}, the developer can get the actual result returned by the interface through res.data, and get the failed return code through res.code.
Taking the storage.get() interface as an example, the code looks like this:
<script> import storage from '@system.storage'; const injectRef = Object.getPrototypeOf(global) || global; / / injection regeneratorRuntime injectRef. RegeneratorRuntime = the 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”} Note:
Await to invoke the need to introduce @ Babel/runtime/regenerator.
For more details, see:
Quick application callback introduction:
https://developer.huawei.com/…
The original link: https://developer.huawei.com/… Author: Mayism