1. function limitLoad(urls, handler, limit) {
    2. const sequence = [].concat(urls)
    3. let promise = []
    4. promise = sequence.splice(0, limit).map((url, index) => {
    5. return handler(url).then(()=>{
    6. return index
    7. })
    8. })
    9. let p = Promise.race(promise)
    10. // for循环给p赋值相当于.then().then()链式调用
    11. for (let i= 0; i< sequence.length; i++) {
    12. p = p.then(res => {
    13. promise[res] = handler(sequence[i]).then(()=>{
    14. return res
    15. })
    16. return Promise.race(promise)
    17. })
    18. }
    19. }
    20. const urls =[
    21. {info:'1', time:2000},
    22. {info:'2', time:1000},
    23. {info:'3', time:2000},
    24. {info:'4', time:2000},
    25. {info:'5', time:3000},
    26. {info:'6', time:1000},
    27. {info:'7', time:2000},
    28. {info:'8', time:2000},
    29. {info:'9', time:3000},
    30. {info:'10', time:1000}
    31. ]
    32. function loadImg(url){
    33. return new Promise((reslove, reject)=>{
    34. console.log(url.info + '---start')
    35. setTimeout(()=>{
    36. console.log(url.info, 'ok!!!')
    37. reslove()
    38. }, url.time)
    39. })
    40. }
    41. limitLoad(urls, loadImg, 3)

    image.png