异步管理的一些面试问题

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    })
 
我站在山顶看风景!下面是我的家乡!
原文地址:https://www.cnblogs.com/zhensg123/p/14802059.html