From 2c6d53fe5305d71acc530bb29bbb8be2546994b9 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Thu, 22 Mar 2018 18:37:27 -0700 Subject: [PATCH] Shim util.promisify since it doesn't exist prior to Node 8 --- index.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index ea54819..5889c1f 100644 --- a/index.js +++ b/index.js @@ -13,8 +13,25 @@ const util = require('util'); const ini = require('ini'); const configPath = require('git-config-path'); const expand = require('expand-tilde'); -const read = util.promisify(fs.readFile); -const stat = util.promisify(fs.stat); + +/** + * Wraps an arbitrary function in a Promise. + * + * @param {Function} `func` The function to be wrapped. + */ + +function promisify(func) { + return function (...args) { + return new Promise((resolve, reject) => { + func(...args, (err, res) => { + err ? reject(err) : resolve(res); + }); + }); + } +} + +const read = promisify(fs.readFile); +const stat = promisify(fs.stat); /** * Asynchronously parse a `.git/config` file. If only the callback is passed,