replaced promises with async/await
This commit is contained in:
parent
53efde189f
commit
ad111f7709
1 changed files with 22 additions and 20 deletions
22
libs/util.js
22
libs/util.js
|
@ -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) {
|
||||||
reject('can not check the existence of an undefined file');
|
throw new Error('can not check the existence of an undefined file');
|
||||||
}
|
}
|
||||||
resolvePath(file)
|
let path;
|
||||||
.then((path) => {
|
try {
|
||||||
|
path = await resolvePath(file);
|
||||||
|
} catch (err) {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
return await new Promise((resolve, reject) => {
|
||||||
stat(path, (err, stats) => {
|
stat(path, (err, stats) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
reject(err);
|
reject(err);
|
||||||
}
|
}
|
||||||
resolve({path, stats});
|
resolve({path, stats});
|
||||||
})
|
})
|
||||||
})
|
|
||||||
.catch(reject);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolvePath(file) {
|
async function resolvePath(file) {
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
if (file === undefined) {
|
if (file === undefined) {
|
||||||
reject('can not resolve a path to an undefined file');
|
throw new Error('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);
|
||||||
|
|
Loading…
Reference in a new issue