• 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()没有先后顺序,那个先请求回来那个先显示

  • 相关阅读:
    通信基石Socket结合OOP实现程序间的通信
    如何将CAD导入GIS并且找到正确的投影坐标
    【Flutter】初识Flutter项目之使用Android Studio创建第一个Flutter项目(详细步骤)
    数据库备份
    C# Winform编程(4)多文档窗口(MDI)
    手把手教你Nginx常用模块详解之ngx_stream_ssl_module(七)
    第3章 语义陷阱
    计算机网络笔记之你这个年纪睡得着觉?(1)
    Cookie和Session
    应约凯程约稿
  • 原文地址:https://blog.csdn.net/qq_36303853/article/details/125886662