/** * Copyright (c) 2017-present A. Matías Quezada */ /** * Decorates a promise-returning-function to cache the last returned value. * The returned function will have a `cached` method which will return the * value used to fulfill the last successful call. * * @example * getServerStatus = syncVersion(getServerStatus); * getServerStatus() * // later... * // if the promise has been fulfilled this will return the value * getServerStatus.cached() * * @param {Function} fn Function to decorate. * @returns {Function} Decorated function */ export default function syncVersion(fn: (...args: any[]) => Promise): ICached { let cache: T = null; const substitute = syncWrapper as ICached; substitute.cached = cached; return substitute; function syncWrapper(...args: any[]) { const promise = fn.apply(this, args) as Promise; promise.then(value => cache = value); return promise; } function cached(): T { return cache; } } export type ICached Promise, U> = T & { cached(): U };