Skip to content

Instantly share code, notes, and snippets.

@ThomRoman
Created March 26, 2021 23:22
Show Gist options
  • Save ThomRoman/5a17c79e0666955505c0a2999e5a7838 to your computer and use it in GitHub Desktop.
Save ThomRoman/5a17c79e0666955505c0a2999e5a7838 to your computer and use it in GitHub Desktop.
FileSystem promise
const fs = require('fs').promises;
async function escribir(path,contenido){
try{
await fs.writeFile(path,contendio)
}catch(err){
console.error(error)
}
}
async function leer(path){
try{
const data = await fs.readFile(path)
return data.toString()
}catch(error){
console.error(error)
}
}
function borrar(path){
try{
fs.unlink(path)
}catch(error){
console.error(error)
}
}
exports.escribir = escribir;
exports.leer = leer;
exports.borrar = borrar;
const fs = require('./file-system')
const CONTENIDO = `
Hola amigos
nueva linea
;
`
async function main(path){
await fs.escribir(path,CONTENIDO)
const data = await fs.leer(path)
console.log(data)
await esperar(path)
}
async function esperar(path){
setTimeout(()=>{
fs.borrar(path)
},3000)
}
main('./archivo.txt')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment