Skip to content

Instantly share code, notes, and snippets.

@avivl
Created April 20, 2022 01:48
Show Gist options
  • Select an option

  • Save avivl/76f6df01227351f7d17faa54d6deef43 to your computer and use it in GitHub Desktop.

Select an option

Save avivl/76f6df01227351f7d17faa54d6deef43 to your computer and use it in GitHub Desktop.
var LRUCache = require('mnemonist/lru-cache');
var getJobPostCache = new LRUCache(1000);
export const getJobPostJson = async (jobId: string) => {
const job = getJobPostCache.get(jobId)
if (job !== undefined) {
return ({
job,
isJobBelongsToThisUser: true
})
}
return _getJobPost(jobId).then((snapshot: any) => {
const docs = snapshot.docs.map((doc: { data: () => any; }) => doc.data());
getJobPostCache.set(jobId, docs )
return (
{ job: docs, isJobBelongsToThisUser: true }
);
}).catch(error => {
logger.error(error)
return ({
status: "error",
message: error.message,
})
})
}
@odedmagger
Copy link

Comments from a stranger on the internet :) .

Lines #1, #2: Use const.

Line #4: No need to declare the function as async if you're not using await inside it.

Line #6: if (job) will do.

Line #9 - does the function ever return an isJobBelongsToUser: false?

Line #12: I'd avoid using an explicit any, and linters can be configured to kick you in the shin if you do it. It's a style decision, though, with a tradeoff of type safety vs. development speed.

Line #20: this means getJobPostJson may return two completely different objects, and whoever calls the function needs to somehow figure out which object type is it.
Consider either re-throwing or add a status field to the 'vanilla' JSON.

@avivl
Copy link
Author

avivl commented Apr 20, 2022

@odedmagger Thank you for your feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment