Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

12345678910111213141516171819202122232425
  1. 'use strict'
  2. const fs = require('fs')
  3. const pify = require('pify')
  4. const pTry = require('p-try')
  5. const pFinally = require('p-finally')
  6. const fsP = pify(fs)
  7. module.exports = (...args) => {
  8. const callback = args.pop()
  9. return fsP
  10. .open(...args)
  11. .then(fd => pFinally(pTry(callback, fd), _ => fsP.close(fd)))
  12. }
  13. module.exports.sync = (...args) => {
  14. const callback = args.pop()
  15. const fd = fs.openSync(...args)
  16. try {
  17. return callback(fd)
  18. } finally {
  19. fs.closeSync(fd)
  20. }
  21. }