This repository has been archived on 2020-11-02. You can view files and clone it, but cannot push or open issues or pull requests.
TripSit_Suite/node_modules/with-open-file/index.js
2020-11-01 22:46:04 +00:00

26 lines
496 B
JavaScript

'use strict'
const fs = require('fs')
const pify = require('pify')
const pTry = require('p-try')
const pFinally = require('p-finally')
const fsP = pify(fs)
module.exports = (...args) => {
const callback = args.pop()
return fsP
.open(...args)
.then(fd => pFinally(pTry(callback, fd), _ => fsP.close(fd)))
}
module.exports.sync = (...args) => {
const callback = args.pop()
const fd = fs.openSync(...args)
try {
return callback(fd)
} finally {
fs.closeSync(fd)
}
}