uniapp - api promise化

uniapp很多api都是异步的,而非同步(asyn),举个例子来说

 

例子1:

  付款步骤:1、2、3,而实际运行可能是1、3、2...

 

 

 

 

例子2:

  通过回调深渊解决它异步问题

 

 

 

例子3:

  uniapp内置async/await,这个的确很棒(封装了request请求情况就会好很多)- 它们有条不紊的进行着

 

 

 

 

但是请思考,uniapp那么多api且很多都是异步的!!! 因此把它们api promise化是必须的(同步)

 

 

1.复制并且命名它

uniPromise.js

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
export default (callback) => {
    return (object = {}) => {
        return new Promise((resolve, reject) => {
            object.success = (...args) => {
                resolve(...args)
            };
            object.fail = (...args) => {
                reject(...args)
            };
            object.complete = () => {}
            callback(object)
        })
    }
};

  

 

 

2.main.js 导入全局

1
2
3
import uniPromise from './uniPormis.js'
 
Vue.prototype.$uni = uniPromise;

  

3.使用它

index.vue

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<template>
    <view>
        uniPromise
    </view>
</template>
 
<script>
    export default {
        data() {
            return {}
        },
        components: {},
        onLoad() {
 
            // 示例1
            this.$uni(uni.getSystemInfo)().then(res => {
                console.log(res)
            })
            .then(()=>   this.$uni(uni.showToast)({
                title:'hello'
            }))
            .then(() =>  this.$uni(uni.showLoading)({
                title: '正在保存'
            }))
            .then(()=>   this.$uni(uni.hideLoading)())
            .then(()=>   this.$uni(uni.showModal)({
                title: 'hello',
                content: 'babbabab',
                showCancel: false,
                cancelText: '取消',
                confirmText: '确定'
            }))
            .then(res=>{
                if(res.confirm){
                    console.log('按了确定')
                }else{
                    console.log('按了取消')
                }
                 
            })
             
             
            // uni.showModal({
            //  title: '',
            //  content: '',
            //  showCancel: false,
            //  cancelText: '',
            //  confirmText: '',
            //  success: res => {},
            //  fail: () => {},
            //  complete: () => {}
            // });
             
             
 
            // 示例2
            // this.$uni(uni.showModal)({
            //  title: 'uniPromise'
            // })
            // .then(res => {
            //  console.log(res)
            //  if (res.cancel) {
            //      // 中断本次运行
            //      console.warn('中断本次运行... 出问题了')
            //      return Promise.reject('这里填写出错的原因,并且中断它');
            //  }
            // })
            // .catch(res=>{
            //  console.log('捕获以后还能运行.. 不捕获直接中断',res);
            // })
            // .then(() => this.$uni(uni.showLoading)({
            //  title: '正在保存'
            // }))
            // .then(() =>{
            //  sleep(3000);
            //  console.log('这里延迟了3s')
            // })
            // .then(() => this.$uni(uni.hideLoading)());
        },
        mounted() {},
        methods: {}
    }
 
    const sleep = numberMillis => {
        let now = new Date();
        let exitTime = now.getTime() + numberMillis;
        while (true) {
            now = new Date();
            if (now.getTime() > exitTime)
                return;
        }
    }
</script>
 
<style>
</style>

 

  

 

 

posted @   Sunsin  阅读(7706)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示