Promise【一】


为实现数据缓存,需要先获取接口返回的数据再进行缓存,这就需要异步转同步,如果不使用异步转筒的话就无法先获取接口返回的数据

 function demo() {
        test()
            console.log('同步内容')

        }

        function test() {
                Axios.get('https://www.fastmock.site/mock/46fde81ecfdc20038b863c7d7a8513ea/test/api/test').then((res) => {
                    console.log(res)
                })
                console.log('同步测试')
        }

 async function demo() {
            await test()
            console.log('同步内容')

        }

        function test() {
            return new Promise(resolve => {
                Axios.get('https://www.fastmock.site/mock/46fde81ecfdc20038b863c7d7a8513ea/test/api/test').then((res) => {
                    console.log(res)
                    resolve(res)
            console.log("同步测试")   })
}) }

 用于项目中实现异步转同步的代码

  HBStorage: {
        // 添加缓存
        setData(param) {
            var json = {};
            json[param.key] = param.value;

            var data = {
                "action": "add",
                "data": json
            };
            console.log('存储入参', data);

            return new Promise((resolve, reject) => {
                window.Hardware.doStorage({
                    data: data,
                    success: function (result) {
                        console.log('储存结果',result, typeof(result.code));
                        var return_callback = {};
                        if (result.code == 0) {
                            return_callback = {
                                "RT_F": '1',
                                "RT_D": "保存成功",
                                "data": result
                            }
                        } else {
                            return_callback = {
                                "RT_F": '0',
                                "RT_D": "保存失败"
                            }
                        }
                        resolve(return_callback);
                    }
                })
            });
        },
        // 读取缓存
        getData(param) {
            var json = {};
            json[param.key] = '';

            var data = {
                "action": "query",
                "data": json
            };

            return new Promise((resolve, reject) => {
                window.Hardware.doStorage({
                    data: data,
                    success: function (result) {
                        var return_callback = {};
                        if (result != undefined) {
                            return_callback = {
                                "RT_F": '1',
                                "RT_D": "查询成功",
                                "data": result[param.key]
                            }
                        } else {
                            return_callback = {
                                "RT_F": '0',
                                "RT_D": "查询失败"
                            }
                        }
                        resolve(return_callback);
                    }
                })
            });
        },
        // 清除缓存
        removeData(param) {
            var json = {};
            json[param.key] = '';

            var data = {
                "action": "remove",
                "data": json
            };

            return new Promise((resolve, reject) => {
                window.Hardware.doStorage({
                    data: data,
                    success: function (result) {
                        var return_callback = {};
                        if (result.code == 0) {
                            return_callback = {
                                "RT_F": '1',
                                "RT_D": "删除成功",
                                "data": result
                            }
                        } else {
                            return_callback = {
                                "RT_F": '0',
                                "RT_D": "删除失败"
                            }
                        }
                        resolve(return_callback);
                    }
                })
            });
        }

    },

使用方法

   (async ()=>{
                            await HBStorage.setData({
                                "fileName": "toWorkOrderList",
                                "key": "toWorkOrderList",
                                "value": JSON.stringify(diyaInfo)
                            });
                        })()