gvwilson / sdxjs

Software Design by Example with JavaScript
Other
49 stars 12 forks source link

Some bugs in code in "file backup" chapter #52

Open masiacra opened 3 weeks ago

masiacra commented 3 weeks ago

Good day. In backup.js there are 2 function in which you use keyword 'await' probably incorrectly. These are copyFiles and saveManifest. You don't use another related keyword 'await' so it is first hint. I think it would be better to omit async in these two functions and return simple promises. In this case you will get:

const copyFiles = (dst, needToCopy) => {
  const promises = Object.keys(needToCopy).map((hash) => {
    const srcPath = needToCopy[hash];
    const dstPath = `${dst}/${hash}.bck`;
    return fs.copyFile(srcPath, dstPath); // you probably miss await in this line
  });

  return Promise.all(promises);
};

const saveManifest = (dst, timestamp, pathHash) => {
  pathHash = pathHash.sort();
  const content = pathHash
    .map(([path, hash]) => {
      return `${path}/${hash}`;
    })
    .join("\n");

  const manifest = `${dst}/${timestamp}.csv`;

  return fs.writeFile(manifest, content, { encoding: "utf-8" }); // and probably miss await in this line
}; 

Thank you for you job and sorry for my poor english

gvwilson commented 3 weeks ago

thanks very much for the bug report @masiacra (and your English is fine :-) ). I'll try to post a fix some time in the next few weeks - cheers!

masiacra commented 2 weeks ago

Thank you for your job. Great book)