cache.set("file2, 'data of file 2...");// Should be (Note: quotes)cache.set("file2","data of file 2...");
This are the changes I'd make
Use object instead of Map (Easier to have immutable structures)
Wrap cache in a Promise.resolve in order to reuse code and be able to use a ternary operator
For consistency DO NOT use a callback syntax for a promise based function
So, the final script would be:
constcache={["file1"]:"data of file 1...",["file2"]:"data of file 2..."};constgetFile=fileName=>{// Wrap the cache in a promise in order to do chaining;constcachePromise=cache[filename]!=null?Promise.resolve(cache):readFilePromise(fileName).then(content=>(cache={...cache,[fileName]:content}));// Return a promise with the datareturncachePromise.then(cache=>cache[fileName]);};// DO NOT pass a callback.getFile("file1").then(data=>{// Do Something}).catch(err=>{// Do Something else});
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Well besides this line ...
This are the changes I'd make
object
instead ofMap
(Easier to have immutable structures)Promise.resolve
in order to reuse code and be able to use a ternary operatorSo, the final script would be: