var data = getData();
data.then((data) => {
console.log(data);
});
// или если все также обернуто в async функцию
var data = await getData();
console.log(data);
const fs = require('fs');
const promiseGetRoot = (startPath) => {
return new Promise((resolve) => {
const f = (path, resolve) => {
fs.access(`${path}/package.json`, fs.constants.F_OK, err => {
if (!err) {
resolve(path);
} else {
const newPath = path.split("/").slice(0, -1).join("/");
f(newPath, resolve);
}
});
};
f(startPath, resolve);
});
};
const callbackGetRoot = (path, callback) => {
fs.access(`${path}/package.json`, fs.constants.F_OK, err => {
if (!err) {
callback(path);
} else {
const newPath = path.split("/").slice(0, -1).join("/");
callbackGetRoot(newPath, callback);
}
})
};
promiseGetRoot(process.env.PWD).then(root => console.log(root));
callbackGetRoot(process.env.PWD, (root) => console.log(root));