解决for循环中异步请求顺序不一致的问题


当前第2页 返回上一页

通过promise.all()方法把promise包装成一个新的promise实例

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

// 通过Promise把所有的异步请求放进事件队列中

getInfo(item ,index) {

    const ms = 1000 * Math.ceil(Math.random() * 3)

    return new Promise((resolve,reject) => {

        setTimeout(() => {

           axios.get(id).then((result) => {

               resolve(result)

           })

        }, ms)

    })

}

 

// 返回多个promise

let promise = arr.map((item,index) = > {

    arr.forEach((item, index) => {

        return getInfo(item, index)

    })

})

// 对返回的promise数组进行操作

Peomise.all(promise).then((allData) => {

    arr.forEach((item, index) => {

        // ......

    })

})

本文来自 js教程 栏目,欢迎学习!

以上就是解决for循环中异步请求顺序不一致的问题的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

详解es6中的for … of循环和可迭代对象

javascript中for in语句怎么用

浅谈input中的name,value以及label中的for

解决for循环中异步请求顺序不一致的问题

更多相关阅读请进入《for》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...