From 1fcd7edcb2a876695733f56763c134299d634b73 Mon Sep 17 00:00:00 2001 From: Paul Melnikow Date: Tue, 7 May 2019 11:34:47 -0400 Subject: [PATCH] Convert to util.promisify and async/await --- index.js | 81 ++++++++++++++++++++-------------------------------- package.json | 1 - 2 files changed, 31 insertions(+), 51 deletions(-) diff --git a/index.js b/index.js index dac83c3..08a493b 100644 --- a/index.js +++ b/index.js @@ -1,66 +1,47 @@ -var tmp = require("tmp"); -var Promise = require("bluebird"); - +const {promisify} = require("util"); +const tmp = require("tmp"); // file module.exports.fileSync = tmp.fileSync; -var file = Promise.promisify(tmp.file, {multiArgs: true}); - -module.exports.file = function file$promise() { - return file.apply(tmp, arguments).spread(function (path, fd, cleanup) { - return {path: path, fd: fd, cleanup : cleanup }; - }); -}; - -module.exports.withFile = function withFile(fn) { - var cleanup; - - var params = Array.prototype.slice.call(arguments, 1); - - return module.exports.file.apply(tmp, params).then(function context(o) { - cleanup = o.cleanup; - delete o.cleanup; - return fn(o); - }).finally(function () { - // May not pass any arguments to cleanup() or it fails. - if (cleanup) { - cleanup(); - } - }); +module.exports.file = promisify( + (options, cb) => tmp.file( + options, + (err, path, fd, cleanup) => cb(err, {path, fd, cleanup}) + ) +); + +module.exports.withFile = async function withFile(fn, options) { + const { path, fd, cleanup } = await module.exports.file(options); + try { + return await fn({ path, fd }); + } finally { + await promisify(cleanup)(); + } }; // directory module.exports.dirSync = tmp.dirSync; -var dir = Promise.promisify(tmp.dir, {multiArgs: true}); - -module.exports.dir = function dir$promise() { - return dir.apply(tmp, arguments).spread(function (path, cleanup) { - return {path: path, cleanup: cleanup}; - }); -}; - -module.exports.withDir = function withDir(fn) { - var cleanup; - - var params = Array.prototype.slice.call(arguments, 1); - - return module.exports.dir.apply(tmp, params).then(function context(o) { - cleanup = o.cleanup; - delete o.cleanup; - return fn(o); - }).finally(function () { - // May not pass any arguments to cleanup() or it fails. - if (cleanup) { - cleanup(); - } - }); +module.exports.dir = promisify( + (options, cb) => tmp.dir( + options, + (err, path, cleanup) => cb(err, {path, cleanup}) + ) +); + +module.exports.withDir = async function withDir(fn, options) { + const { path, cleanup } = await module.exports.dir(options); + try { + return await fn({ path }); + } finally { + await promisify(cleanup)(); + } }; // name generation module.exports.tmpNameSync = tmp.tmpNameSync; -module.exports.tmpName = Promise.promisify(tmp.tmpName); +module.exports.tmpName = promisify(tmp.tmpName); module.exports.tmpdir = tmp.tmpdir; diff --git a/package.json b/package.json index e618096..15a0df4 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,6 @@ "url": "git://github.com/benjamingr/tmp-promise.git" }, "dependencies": { - "bluebird": "^3.5.0", "tmp": "0.1.0" }, "devDependencies": {