Skip to content

Instantly share code, notes, and snippets.

@twick00
Created December 1, 2019 20:52
Show Gist options
  • Save twick00/dd74ebaee0d6722529beb90f406eca43 to your computer and use it in GitHub Desktop.
Save twick00/dd74ebaee0d6722529beb90f406eca43 to your computer and use it in GitHub Desktop.
export function useCacheable<T, H>(
filePath: PathLike,
promiseFunction: (args: H) => Promise<T>
): (args: H) => Promise<T> {
return async (...args: [H]) => {
createFileIfNoteExist(filePath)
return new Promise(resolve => {
return promiseFunction(...args).then(
value => {
if (!isEmpty(value)) {
writeFile(filePath, value, err => {
if (err) {
throw err
}
})
resolve(value)
}
return value
},
_rejected => {
readFile(filePath, (err, data) => {
const output = JSON.parse(data.toString('utf8'))
resolve(output)
})
}
)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment