The fast application asynchronous interface supports the way of returning Promise (1010+), and developers write code with async and await methods to achieve synchronization effect. For an interface call that returns an object res = {data} on success, the developer can use res.data to get the actual result returned by the interface, and res.code to get the return code on failure.

Take the storage.get() interface as an example, the code is as follows:

<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>Copy the code

The output is as follows:

getValue re={"data":"hanmeimei"}
Copy the code

Matters needing attention:

Await to invoke the need to introduce @ Babel/runtime/regenerator.

For more details, see:

Quick application callback introduction:

Developer.huawei.com/consumer/cn…

The original link: developer.huawei.com/consumer/cn…

Original author: Mayism