• vue实现循环发起多个异步请求——Promise.all()与Promise.race()


    直接先上个例子

    <template>
        <div>
            <h1>page1h1>
            <p>{{ msg }}p>
            <el-button type="primary" plain @click="downloadList()">主要按钮el-button>
        div>
    template>
    <script>
    export default {
        data() {
            return {
                msg: "我是page1组件",
                pagnation:{
                    total:1000,
                }
            }
        },
        methods: {
            downloadList(num) {
                let array = []
                // 计算可以循环多少次
                let n = 1
                if (this.pagnation.total < 100) {
                    n = 1
                } else {
                    n = Math.ceil(this.pagnation.total / 100)
                }
                for (let i = 0; i < n; i++) {
                    array = array.concat(this.ForPromise(i))
                }
                Promise.all(array).then((res) => {
                    console.log(res) // [ 0, 1, 2 ]
                })
            },
            ForPromise(num) {
                return new Promise((resolve, reject) => {
                    resolve('成功了'+num);
                    //请求操作
                    // this.axios.get('http://test.fastadmin.cn/index.php/api/index/test',{}).then(res => {}).catch(err => {});
                })
            },
            sleep(ms) { //sleep延迟方法2
                var unixtime_ms = new Date().getTime();
                while (new Date().getTime() < unixtime_ms + ms) { }
            },
    
        }
    }
    script>
    
    
    • 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

    结果

    在这里插入图片描述

    说明

    Promise.all 在任意一个传入的 promise 失败时返回失败。例如,如果你传入的 promise中,有四个 promise
    在一定的时间之后调用成功函数,有一个立即调用失败函数,那么 Promise.all 将立即变为失败。

    Promise.race()的使用

    var p1 = new Promise(function(resolve, reject) {
     	setTimeout(resolve, 500, "one");
    });
    var p2 = new Promise(function(resolve, reject) {
        setTimeout(resolve, 100, "two");
    });
    
    Promise.race([p1, p2]).then(function(value) {
      	console.log(value); // "two"
      	// 两个都完成,但 p2 更快
    });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    3.Promise.all()与Promise.race()请求时的区别

    Promise.all() 适合于后面的异步请求接口依赖前面的接口请求的数据时使用。
    Promise.race()没有先后顺序,那个先请求回来那个先显示

  • 相关阅读:
    路由配置与mongoose模型构建
    直播录屏软件哪个好?什么软件可以录屏直播会议?
    Servlet--Request请求对象
    github-actions
    数据可视化素材分享 | 数十图表、无数模板
    java计算机毕业设计废旧物品回收管理系统MyBatis+系统+LW文档+源码+调试部署
    人生苦短,我用JRebel
    python 斐波那契数列多种方法
    2024年大语言模型的微调
    IPv6知识点整理
  • 原文地址:https://blog.csdn.net/qq_36303853/article/details/125886662