当前位置:   article > 正文

JS手写——实现Promise的最大并发数控制_js promise 最多 请求并发

js promise 最多 请求并发

 一、不使用Promise.race

维护一个并发池pool,先将其填满,然后每当其中的promise完成后添加新的promise,直到并发队列清空。

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. </body>
  11. <script>
  12. let request = options =>{
  13. return new Promise((resolve,reject)=>{
  14. setTimeout(()=>{resolve(options.url)},options.delay)
  15. }).then((res)=>{
  16. console.log(`${options.url}任务完成`)
  17. })
  18. }
  19. let now = new Date()
  20. function addTask(options){
  21. let task = request(options)
  22. pool.push(task)
  23. task.then((res)=>{
  24. pool.splice(pool.indexOf(task),1)
  25. newOptions = requests.shift()
  26. if(newOptions){
  27. console.log(`${options.url}任务完成,耗时${(new Date()-now)/1000}秒,${newOptions.url}开始执行,预计耗时${newOptions.delay/1000}秒`)
  28. addTask(options)
  29. }else{
  30. console.log(`${options.url}任务完成,耗时${(new Date()-now)/1000}秒`)
  31. }
  32. })
  33. }
  34. let requests = [{
  35. url:'tencent',
  36. delay:1000
  37. },{
  38. url:'baidu',
  39. delay:3000
  40. },{
  41. url:'bytedance',
  42. delay:2000
  43. },{
  44. url:'tieba',
  45. delay:1000
  46. },{
  47. url:'bilibili',
  48. delay:1000
  49. },{
  50. url:'acfun',
  51. delay:1000
  52. },{
  53. url:'meituan',
  54. delay:1000
  55. }]
  56. let pool = []
  57. let max = 3
  58. while(pool.length<max){
  59. let options = requests.shift()
  60. addTask(options)
  61. }
  62. </script>
  63. </html>

二、使用Promise.race

原理类似,每当Promise.race()返回结果,就可以执行下一个promise了。

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. </body>
  11. <script>
  12. let request = options =>{
  13. return new Promise((resolve,reject)=>{
  14. setTimeout(()=>{resolve(options.url)},options.delay)
  15. }).then((res)=>{
  16. console.log(`${options.url}任务完成`)
  17. })
  18. }
  19. let now = new Date()
  20. function addTask(options){
  21. let task = request(options)
  22. pool.push(task)
  23. task.then((res)=>{
  24. console.log(`${options.url}任务完成,耗时${(new Date()-now)/1000}秒`)
  25. pool.splice(pool.indexOf(task),1)
  26. })
  27. }
  28. function run(race){
  29. race.then((res)=>{
  30. let newOptions = requests.shift()
  31. if(newOptions){
  32. console.log(`${newOptions.url}开始执行,预计耗时${newOptions.delay/1000}秒`)
  33. addTask(newOptions)
  34. run(Promise.race(pool))
  35. }
  36. })
  37. }
  38. let requests = [{
  39. url:'tencent',
  40. delay:1000
  41. },{
  42. url:'baidu',
  43. delay:3000
  44. },{
  45. url:'bytedance',
  46. delay:2000
  47. },{
  48. url:'tieba',
  49. delay:1000
  50. },{
  51. url:'bilibili',
  52. delay:1000
  53. },{
  54. url:'acfun',
  55. delay:1000
  56. },{
  57. url:'meituan',
  58. delay:1000
  59. }]
  60. let pool = []
  61. let max = 3
  62. while(pool.length<max){
  63. let options = requests.shift()
  64. addTask(options)
  65. }
  66. let race = Promise.race(pool) // 返回最先完成的promise
  67. run(race)
  68. </script>
  69. </html>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/271743
推荐阅读
相关标签
  

闽ICP备14008679号