packages/data-point-service/lib/cache-middleware.js
function resolveStaleWhileRevalidateEntry (service, entryKey, cache, ctx) {
  // NOTE: pulling through module.exports allows us to test if they were called
  const {
    resolveStaleWhileRevalidate,
    isRevalidatingCacheKey,
    shouldTriggerRevalidate,
    revalidateEntry
  } = module.exports

  // IMPORTANT: we only want to bypass an entity that is being revalidated and
  // that matches the same cache entry key, otherwise all child entities will
  // be needlesly resolved
  if (isRevalidatingCacheKey(ctx, entryKey)) {
    // bypass the rest forces entity to get resolved
    return undefined
  }

  const staleWhileRevalidate = resolveStaleWhileRevalidate(service)

  // cleanup on a new tick so it does not block current process, the clear
  // method is throttled for performance
  setTimeout(staleWhileRevalidate.invalidateLocalFlags, 0)

  const tasks = [
    staleWhileRevalidate.getRevalidationState(entryKey),
    staleWhileRevalidate.getEntry(entryKey)
  ]

  return Promise.all(tasks).then(results => {
    const revalidationState = results[0]
    const staleEntry = results[1]

    if (shouldTriggerRevalidate(staleEntry, revalidationState)) {
      // IMPORTANT: revalidateEntry operates on a new thread
      revalidateEntry(service, entryKey, cache, ctx)
    } // Otherwise means its a cold start and must be resolved outside

    // return stale entry regardless
    return staleEntry
  })
}
function resolveStaleWhileRevalidateEntry (service, entryKey, cache, ctx) {
  // NOTE: pulling through module.exports allows us to test if they were called
  const {
    resolveStaleWhileRevalidate,
    isRevalidatingCacheKey,
    shouldTriggerRevalidate,
    revalidateEntry
  } = module.exports

  // IMPORTANT: we only want to bypass an entity that is being revalidated and
  // that matches the same cache entry key, otherwise all child entities will
  // be needlesly resolved
  if (isRevalidatingCacheKey(ctx, entryKey)) {
    // bypass the rest forces entity to get resolved
    return undefined
  }

  const staleWhileRevalidate = resolveStaleWhileRevalidate(service)

  // cleanup on a new tick so it does not block current process, the clear
  // method is throttled for performance
  setTimeout(staleWhileRevalidate.invalidateLocalFlags, 0)

  const tasks = [
    staleWhileRevalidate.getRevalidationState(entryKey),
    staleWhileRevalidate.getEntry(entryKey)
  ]

  return Promise.all(tasks).then(results => {
    const revalidationState = results[0]
    const staleEntry = results[1]

    if (shouldTriggerRevalidate(staleEntry, revalidationState)) {
      // IMPORTANT: revalidateEntry operates on a new thread
      revalidateEntry(service, entryKey, cache, ctx)
    } // Otherwise means its a cold start and must be resolved outside

    // return stale entry regardless
    return staleEntry
  })
}
function resolveStaleWhileRevalidateEntry ( service , entryKey , cache , ctx ) { // NOTE: pulling through module.exports allows us to test if they were called const { resolveStaleWhileRevalidate , isRevalidatingCacheKey , shouldTriggerRevalidate , revalidateEntry } = module . exports // IMPORTANT: we only want to bypass an entity that is being revalidated and // that matches the same cache entry key, otherwise all child entities will // be needlesly resolved if ( isRevalidatingCacheKey ( ctx , entryKey ) ) { // bypass the rest forces entity to get resolved return undefined } const staleWhileRevalidate = resolveStaleWhileRevalidate ( service ) // cleanup on a new tick so it does not block current process, the clear // method is throttled for performance setTimeout ( staleWhileRevalidate . invalidateLocalFlags , 0 ) const tasks = [ staleWhileRevalidate . getRevalidationState ( entryKey ) , staleWhileRevalidate . getEntry ( entryKey ) ] return Promise . all ( tasks ) . then ( results => { const revalidationState = results [ 0 ] const staleEntry = results [ 1 ] if ( shouldTriggerRevalidate ( staleEntry , revalidationState ) ) { // IMPORTANT: revalidateEntry operates on a new thread revalidateEntry ( service , entryKey , cache , ctx ) } // Otherwise means its a cold start and must be resolved outside // return stale entry regardless return staleEntry } ) }
If stale entry exists and control entry has expired it will fire a new thread
to resolve the entity, this thread is not meant to be chained to the main
Promise chain.

When this function returns undefined it is telling the caller it should
make a standard resolution of the entity instead of using the
stale-while-revalidate process.

@param {Service} service Service instance
@param {String} entryKey entry key
@param {Object} cache cache configuration
@param {DataPoint.Accumulator} ctx DataPoint Accumulator object
@returns {Promise<Object|undefined>} cached stale value
If stale entry exists and control entry has expired it will fire a new thread to resolve the entity this thread is not meant to be chained to the main Promise chain .