From ad111f77092687b35c4047b61c5a281cfb14b03b Mon Sep 17 00:00:00 2001 From: velvettear Date: Thu, 24 Feb 2022 10:31:01 +0100 Subject: [PATCH] replaced promises with async/await --- libs/util.js | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/libs/util.js b/libs/util.js index cf7ccb8..e164408 100644 --- a/libs/util.js +++ b/libs/util.js @@ -1,29 +1,31 @@ const realpath = require('fs').realpath; const stat = require('fs').stat; -function fileExists(file) { - return new Promise((resolve, reject) => { - if (file === undefined) { - reject('can not check the existence of an undefined file'); - } - resolvePath(file) - .then((path) => { - stat(path, (err, stats) => { - if (err) { - reject(err); - } - resolve({ path, stats }); - }) - }) - .catch(reject); +async function fileExists(file) { + if (file === undefined) { + throw new Error('can not check the existence of an undefined file'); + } + let path; + try { + path = await resolvePath(file); + } catch (err) { + throw err; + } + return await new Promise((resolve, reject) => { + stat(path, (err, stats) => { + if (err) { + reject(err); + } + resolve({path, stats}); + }) }); } -function resolvePath(file) { - return new Promise((resolve, reject) => { - if (file === undefined) { - reject('can not resolve a path to an undefined file'); - } +async function resolvePath(file) { + if (file === undefined) { + throw new Error('can not resolve a path to an undefined file'); + } + return await new Promise((resolve, reject) => { realpath(file, (err, resolvedPath) => { if (err) { reject('resolving path \'' + file + '\' encountered an error >>> ' + err);