Skip to content

Instantly share code, notes, and snippets.

@sethdavis512
Last active November 13, 2024 15:36
Show Gist options
  • Save sethdavis512/4d8eba44076fc85fd81aa9f4d7035e28 to your computer and use it in GitHub Desktop.
Save sethdavis512/4d8eba44076fc85fd81aa9f4d7035e28 to your computer and use it in GitHub Desktop.
Node read and write async functions
const read = async (filePath: string) => {
return new Promise((resolve, reject) => {
fs.readFile(filePath, 'utf8', (err: any, data: string) => {
if (err) reject(err)
resolve(data)
})
})
}
const write = (filePath: string, fileName: string, fileExtension: string, content: any) => {
return new Promise((resolve, reject) => {
fs.writeFile(`${filePath}/${fileName}.${fileExtension}`, content, (err: any) => {
if (err) reject(err)
console.log('\n💥SUCCESS\n')
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment