Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Utility Function "catchAsync" for Simplified Error Handling in Asynchronous Operations #39

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { promises as fs ,existsSync} from "fs";
import {createIfNot} from "./utils/fileUtils.js"
import * as path from "node:path"
async function writeSQL(statement: string, saveFileAs = "", isAppend: boolean = false) {
try {
import { catchAsync } from "./utils/catchAsync.js";

const writeSQL = catchAsync(async function (statement: string, saveFileAs = "", isAppend: boolean = false) {
const destinationFile = process.argv[2] || saveFileAs;
if (!destinationFile) {
throw new Error("Missing saveFileAs parameter");
Expand All @@ -13,13 +14,9 @@ async function writeSQL(statement: string, saveFileAs = "", isAppend: boolean =
}else{
await fs.writeFile(`sql/${process.argv[2]}.sql`, statement);
}
} catch (err) {
console.log(err);
}
}
});

async function readCSV(csvFileName = "", batchSize: number = 0) {
try {
const readCSV = catchAsync(async function (csvFileName = "", batchSize: number = 0) {
const fileAndTableName = process.argv[2] || csvFileName;

batchSize = parseInt(process.argv[3]) || batchSize || 500;
Expand Down Expand Up @@ -87,9 +84,8 @@ async function readCSV(csvFileName = "", batchSize: number = 0) {
const sqlStatement = beginSQLInsert + values;
// Write File
writeSQL(sqlStatement, fileAndTableName, isAppend);
} catch (err) {
console.log(err);
}
}
})


readCSV();
console.log("Finished!");
10 changes: 10 additions & 0 deletions src/utils/catchAsync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type AsyncCallback<T> = (...args : any[]) => Promise<T>

export const catchAsync = <T>(fn : AsyncCallback<T>) =>{
return (...args : any []) => {
fn(...args)
.catch((err) => {
console.error(err);
});
};
};