1.0.2 • Published 3 years ago

async-throttle-exec v1.0.2

Weekly downloads
3
License
ISC
Repository
github
Last release
3 years ago

安装

npm install async-throttle-exec --save

导入

import AsyncThrottle from 'async-throttle-exec'

实例化

// 传入返回Promise对象的执行函数作为参数
const getUser = new AsyncThrottle(() => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve({ name: 'Joseph' })
    }, 2000)
  })
})

异步节流执行

getUser.throttleExec().then(user => {
  // user => { name: 'Joseph' }
})

// 多次调用, 实例化传入的执行函数只会执行一次, .then方法里的回调函数又能全部执行
getUser.throttleExec().then()
getUser.throttleExec().then()

使用缓存

getUser.throttleExec({ key: 'user_info', cacheType: 'instance' }).then()
  • @param1 { object } options

    • { string } key 'key' 缓存key
    • { false | 'instance' | 'sessionStorage' | 'localStorage' } cacheType false 缓存类型(false: 不使用缓存)
  • @param2 @param3 ...

    • 第二参数开始的其它形参是实例化传入的执行函数的形参

实例化设置默认参数

const options = {
  key: 'user_info',
  cacheType: 'sessionStorage',
  namespaces: 'user'
}
const getUser = new AsyncThrottle(() => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve({ name: 'Joseph' })
    }, 2000)
  })
}, options)
  • @param1 { function } execFunction 执行函数

  • @param2 { object } options

    • { string } key 'key' 缓存key
    • { false | 'instance' | 'sessionStorage' | 'localStorage' } cacheType false 缓存类型(false: 不使用缓存)
    • { string } namespaces 命名空间(使用 sessionStorage/localStorage 缓存必须设置 命名空间)

清除缓存

getUser.clearCache(key)
  • @param { string } key 被清除缓存key

更多使用场景介绍

https://juejin.cn/post/6876486644581531662

1.0.2

3 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.0.1

4 years ago