Created
March 26, 2021 23:22
-
-
Save ThomRoman/5a17c79e0666955505c0a2999e5a7838 to your computer and use it in GitHub Desktop.
FileSystem promise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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