shaun
shaun

Reputation: 1273

Trying to understand Await/Async, is this a correct conversion?

So I am trying to rewrite the following callback function using the async/await pattern in node 8.9.4. Do I have it right or should it be rewritten further?

const fs = require("fs");

const makeBackup = (file, callback) => {
    if(fs.existsSync(`${file}.bak`)) return callback(null, true);
    fs.copyFile(file, `${file}.bak`, cpErr => {
        if(cpErr && cpErr.code === "ENOENT"){
            fs.writeFile(`${file}.bak`, Buffer.alloc(0), writeErr => {
                if(writeErr) return callback(writeErr);
                return callback(null, true);
            });
        } else if(cpErr) return callback(cpErr);
        return callback(null, true);
    })
}

Here is the async/await function I have come up with.

import fs, { copyFile, writeFile } from "fs";
import { promisify } from "util";

const makeBackup = async file => {
  // if backup file exists already do nothing further return true.
  if (fs.existsSync(`${file}.bak`)) return true;
  try {
    copyFile = promisify(fs.copyFile);
    // if backup file doens't exist copy file to file.bak
    await copyFile(file, `${file}.bak`);
    // return true when file is copied to bakup file.
    return true;
  } catch (cpErr) {
    // if file missing create an empty backup file
    if (cpErr.code === "ENOENT") {
      try {
        writeFile = promisify(fs.writeFile);
        await writeFile(`${file}.bak`, Buffer.alloc(0));
        return true;
      } catch (writeErr) {
        // if error writing to file for any reason return error
        return writeErr;
      }
    }
    // if copy error for any other reason return error
    return cpErr;
  }

Upvotes: 2

Views: 205

Answers (1)

Orelsanpls
Orelsanpls

Reputation: 23565

I would break it into two functions. It's easier to read, and there is less of try/catch.

My own rule is when I see nested try/catch or nested Array functions it means that you have to create separated functions.

     async makeBackup(file) {
        // if backup file exists already do nothing further return true.
        if (fs.existsSync(`${file}.bak`)) return true;

        try {
          copyFile = promisify(fs.copyFile);

          // if backup file doens't exist copy file to file.bak
          await copyFile(file, `${file}.bak`);

          // return true when file is copied to bakup file.
          return true;
        } catch (cpErr) {
          // if file missing create an empty backup file
          if (cpErr && cpErr.code === "ENOENT") {
            return createEmptyBackupFile(file);
          }

          // if copy error for any other reason return error
          return cpErr;
        }
     }

     async createEmptyBackupFile(file) {
        writeFile = promisify(fs.writeFile);

        // if error writing to file for any reason it will throw an error
        await writeFile(`${file}.bak`, Buffer.alloc(0));

        return true;
     }

Upvotes: 2

Related Questions