replaced promises with async/await

This commit is contained in:
Daniel Sommer 2022-02-24 10:31:01 +01:00
parent 53efde189f
commit ad111f7709

View file

@ -1,29 +1,31 @@
const realpath = require('fs').realpath; const realpath = require('fs').realpath;
const stat = require('fs').stat; const stat = require('fs').stat;
function fileExists(file) { async function fileExists(file) {
return new Promise((resolve, reject) => { if (file === undefined) {
if (file === undefined) { throw new Error('can not check the existence of an undefined file');
reject('can not check the existence of an undefined file'); }
} let path;
resolvePath(file) try {
.then((path) => { path = await resolvePath(file);
stat(path, (err, stats) => { } catch (err) {
if (err) { throw err;
reject(err); }
} return await new Promise((resolve, reject) => {
resolve({ path, stats }); stat(path, (err, stats) => {
}) if (err) {
}) reject(err);
.catch(reject); }
resolve({path, stats});
})
}); });
} }
function resolvePath(file) { async function resolvePath(file) {
return new Promise((resolve, reject) => { if (file === undefined) {
if (file === undefined) { throw new Error('can not resolve a path to an undefined file');
reject('can not resolve a path to an undefined file'); }
} return await new Promise((resolve, reject) => {
realpath(file, (err, resolvedPath) => { realpath(file, (err, resolvedPath) => {
if (err) { if (err) {
reject('resolving path \'' + file + '\' encountered an error >>> ' + err); reject('resolving path \'' + file + '\' encountered an error >>> ' + err);