zoukankan      html  css  js  c++  java
  • 异步管理的一些面试问题

    1,最大并行请求数固定,比如2个,方案是记录当前并行的数量curcount,利用递归持续发出请求

     1  class asyncPromise {
     2             constructor() {
     3                 this.maxcount = 2  // 最大数
     4                 this.curcont = 0  // 当前请求数
     5                 this.promiseArr = []
     6             }
     7 
     8             taskStart() {
     9                 if (!this.promiseArr.length) {
    10                     return false
    11                 }
    12                 for (let i = 0; i < this.maxcount; i++) {
    13                     this.start()
    14                 }
    15             }
    16 
    17             start() {
    18                 if (this.curcont > this.maxcount) {
    19                     return false
    20                 }
    21                 console.log(this.curcont, '322')
    22                 this.curcont++;
    23                 let p = this.promiseArr.pop()
    24                 p && p.then((res) => {
    25                     this.curcont--
    26                     this.start()  // 递归
    27                 })
    28             }
    29 
    30             addPromise(p) {
    31                 this.promiseArr.push(p)
    32             }
    33         }
    2,点击事件用节流处理
     1  const synchronousFile = function (id) {
     2             console.log(id)
     3         }
     4         const proxySynchronousFile = (function () {
     5             let cache = [],
     6                 timer = null
     7             return function (id) {
     8                 cache.push(id)
     9                 if (timer) {
    10                     return false
    11                 }
    12                 timer = setTimeout(() => {
    13                     synchronousFile(cache.join(','))
    14                     clearTimeout(timer)
    15                     timer = null
    16                     cache = []
    17                 }, 2000)
    18             }
    19         })();
    3, 实现一个allSettled
    方案一是利用遍历,保存每个结果当请求结果时resolve
    // 方案1
    1
    Promise.allSettled = function (promiseArr) { 2 return new Promise((resolve, reject) => { 3 const len = promiseArr.length, 4 resArr = [] 5 for (let i = 0; i < len; i++) { 6 promiseArr[i].then((res) => { 7 resArr.push(res) 8 console.log(i, 'ddddddd') 9 }).catch((err) => { 10 resArr.push(err) 11 }).finally(() => { 12 if (len === i + 1) { 13 resolve(resArr) 14 } 15 }) 16 } 17 }) 18 }

    方案二是利用Promise.all,将Promise.all的运行的promise包一层,将其全部返回正确的onFulfilled状态

     1  const rejectHandler = reason => ({
     2             status: "rejected",
     3             reason
     4         })
     5         const resolveHandler = value => ({
     6             status: "fulfilled",
     7             value
     8         })
     9         Promise.allSettled = promises =>
    10             Promise.all(
    11                 promises.map((promise) =>{
    12                    return  promise.then(resolveHandler, rejectHandler)
    13                 })
    14                 // 每个 promise 需要用 Promise.resolve 包裹下
    15                 // 以防传递非 promise
    16             );
    4, 异步请求缓存 ,方案利用了函数名的静态对象
     1  function ajax (name) {
     2        return new Promise((resolve, reject) =>{
     3            setTimeout(() => {
     4                resolve({name: name, age: '27'})
     5            }, 2000)
     6        })
     7    }
     8    function query (name) {
     9        const cache = query.cache || (query.cache = new Map())
    10        if (cache.has(name)) {
    11            console.log('走缓存了')
    12            return Promise.resolve(cache.get(name))
    13        }
    14        return ajax(name).then((res) => {
    15            cache.set(name, res)
    16            console.log(cache)
    17            return res
    18        })
    19    }
    20    query ('wayke').then((res) => {
    21        console.log(res)
    22    })
     
    我站在山顶看风景!下面是我的家乡!
  • 相关阅读:
    使用mongodb保存爬取豆瓣电影的数据
    使用scrapy爬取阳光热线问政平台
    使用scrapy爬取手机版斗鱼主播的房间图片及昵称
    使用selenium + chrome爬取中国大学Mooc网的计算机学科的所有课程链接
    使用scrapy爬取腾讯社招,获取所有分页的职位名称及chaolia、类型、人数、工作地点、发布日期超链接
    python2使用bs4爬取腾讯社招
    使用python2爬取百度贴吧指定关键字和分页帖子楼主所发的图片
    提问的智慧
    深刻理解系统架构师和系统分析师定义
    Redis基础数据结构与核心原理
  • 原文地址:https://www.cnblogs.com/zhensg123/p/14802059.html
Copyright © 2011-2022 走看看