Background Promise
A simple cache that will load the value on demand. During a load process, if the cache receive multiples requests, it will push it to a queue, and trigger all with the response when the request receive the response.
Options
load(resolve, reject) {...}
: Function used to load a new value requiredimmediate
: Should load the value in background during the creation. (true | false)ttl
: Time in millis, that will keep the value. If it expires, the firstget()
call will load a new one.interval
: Time in millis that will load a new value in background.promise
: Promise type that should be used internally to create all promises.
Methods
get()
: Return aPromise
to the value.getLast()
: Return aPromise
to the value, and avoid the queue, return the last loaded value!update()
: Force an update on the cache.isExpired()
: What the name says!
Simple example
const content = // the first call will trigger the executor to resolve the numbercontent// Will go to the queue and wait for the same responsecontent // after 1 secondcontent
Example to cache a request
const content = // the first call will trigger the executor to resolve the requestcontent // Will always get the last loaded content!// if it is expired, it will trigger the update before the intervalcontent // Will get the cached content!content