0.1.0 • Published 4 years ago
memo-async-fn v0.1.0
memo-async-fn
combine async / promise calls and cache result in memory with LRU
import memoAsyncFn from 'memo-async-fn'
const getUserInfo = memoAsyncFn(
async (userId) => {
const { data } = await fetcher('http://xxx/', { userId })
return data
}
)
const user1 = await getUserInfo(12) // send request
const user2 = await getUserInfo(12) // merged to 1st request
const user3 = await getUserInfo(9) // send request
const user4 = await getUserInfo(12) // merged too
// after few seconds...
const user5 = await getUserInfo(12) // get cached result
API
this package provides:
memoAsyncFn(fn, opts)
return an enhanced async functionopts
: optional. seeMemoOptions
in <./src/memoAsyncFn.ts>
combined(opts)
class method decoratorNote: every instance has its own LRU cache in memory. if you have many instances, consider using same cache and setting
opts.cache
andopts.genKey
@example
class MyTest { @combined() async fetchInfo(userId) { // some expensive requests } } const tester = new MyTest() // now tester.fetchInfo is enhanced by memoAsyncFn
opts
: optional. seeMemoOptions
in <./src/memoAsyncFn.ts>
0.1.0
4 years ago