Created
December 1, 2019 20:52
-
-
Save twick00/dd74ebaee0d6722529beb90f406eca43 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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