Calling asynchronous recursive functions in Node.js with Promise - node.js

at first i need to say im pretty new to electron and node.js.
What i am doing is im trying to send an array with some directory data to my browser and this asynchronous (Error: array is empty).
The Problem should be my function 'scanDirectories(path)'.
If i make it non recursive (scanDirectories(res) -> only return res) it works pretty fine for 1 level directories, but recursive it doesnt.
So i think i need to do it like my function above with returning a new promise?
I tried it but can´t figure out how it works or my syntax should be.
Hope you can help me.
Simon
main.js:
calling function from ipcmain
ipcMain.on('fileTree', (event, arg) => {
let fileDirectory = helperFile.getDirectories(arg);
fileDirectory.then(function(result) {
event.reply('fileTree', result);
}, function(err) {
console.log(err);
})
})
files.js
const { readdir } = require('fs').promises;
const resolvePath = require('path').resolve;
module.exports = {
getDirectories: async function(path) {
return new Promise(function (resolve, reject) {
try {
resolve(scanDirectories(path));
} catch {
reject('Error');
}
});
}
};
async function scanDirectories(path) {
const dirents = await readdir(path, {withFileTypes: true});
const files = dirents.map((dirent) => {
const res = resolvePath(path, dirent.name);
return dirent.isDirectory() ? scanDirectories(res) : res;
});
return Array.prototype.concat(...files);
}

You can try something like this where you generate an array of promises:
files.js
const { readdir } = require('fs').promises;
const resolvePath = require('path').resolve;
module.exports = {
// This is an async function so don’t need internal promise
getDirectories: async function(path) {
try {
const dirs = await scanDirectories(path);
return dirs;
}
catch {
throw new Error('Error');
}
}
};
async function scanDirectories(path) {
const dirents = await readdir(path, {withFileTypes: true});
// Generate array of promises
const promises = dirents.map(dirent => {
const res = resolvePath(path, dirent.name);
return dirent.isDirectory()
? scanDirectories(res)
: Promise.resolve(res);
});
// Await all promises
const files = await Promise.all(promises);
return Array.prototype.concat(...files);
}

If you call an async function without await, you receive a promise.
Your event handler handles this sort-of-OK with then (it has trouble with error handling), but your recursive call to scanDirectories does not.
The simplest way to wait for an async function to resolve is to use await.
So this change makes the recursive call properly:
return dirent.isDirectory() ? (await scanDirectories(res)) : res;
Note the addition of await.
However "Array.map" is not designed for use in async functions. It will call them synchronously and create an array of promises. Not what you want.
In addition, this code is unnecessarily complicated, wrapping a promise in a promise and using try/catch in a way that won't work:
getDirectories: async function(path) {
return new Promise(function (resolve, reject) {
try {
resolve(scanDirectories(path));
} catch {
reject('Error');
}
});
}
Just call scanDirectories directly from your original event handler, and make the event handler an async function, so a lot of code just goes away.
In general: if you have to deal with async stuff, write an async function, and always await it in the function that calls it, even if that function is itself. You may write async functions anywhere, even if they are event handlers or Express routes or other contexts where the promise they resolve to won't be consumed.
Here is your original code simplified and corrected but working basically the same way:
ipcMain.on('fileTree', async (event, arg) => {
try {
event.reply('fileTree', await helperFile.scanDirectories(arg);
} catch (e) {
console.log(e);
}
});
// files.js
const { readdir } = require('fs').promises;
const resolvePath = require('path').resolve;
module.exports = {
scanDirectories: async function(path) {
const dirents = await readdir(path, { withFileTypes: true });
const files = [];
for (const dirent of dirents) {
const res = resolvePath(path, dirent.name);
if (dirent.isDirectory()) {
files = files.concat(await scanDirectories(res));
} else {
files.push(res);
}
});
return files;
}
}

Related

async/await troubles in a recursive Redis function

Ima rookie using async/await but must now to use Redis-om. NN_walkd walks through a Redis database looking for loop-chains and does this by recursion. So the 2 questions I have is:
Am I calling the inner recursive NN_walkd calls correctly via async/await?
At runtime, the compSearchM proc is called first and seems to work (it gets 5 entries so it has to call NN_walkd 5 times). A NN_walkd is then recursively called, and then when it loops the 1st time it then calls compSearchK where the problems are. It seems to sit on the first Redis call in compSearchK (.search). Yet the code for compSearchK and compSearchM look basically identical.
main call
NN_walk = async function(req, db, cnode, pnode, chain, cb) {
var vegas, sneaker;
req.session.walk = [];
await NN_walkd(req, cnode, pnode, [], 1);
req.session.walk = null;
console.log('~~~~~~~~~~~~ Out of Walk ~~~~~~~~~~~~~~~');
cb();
};
redis.mjs
export class RedisDB {
constructor() {
...
this._companyRepo = ...
}
compSearchK(ckey) { // doesn't matter if I have a async or not here
return new Promise(async (resolve) => {
const sckey = await this._companyRepo.search()
.where('COMPANYKEY').equals(ckey)
.return.all();
if (sckey.length) {
const ttrr = await this._companyRepo.fetch(sckey[0].entityId);
resolve(ttrr.toJSON());
} else
resolve(null);
});
}
compSearchM(mkey) {
var tArr=[];
return new Promise(async (resolve) => {
const smkey = await this._companyRepo.search()
.where('MASTERKEY').equals(mkey)
.and('TBLNUM').equals(10)
.return.all();
if (smkey.length) {
for (var spot in smkey) {
const ttrr = await this._companyRepo.fetch(smkey[spot].entityId);
tArr.push(ttrr.toJSON());
}
resolve(tArr);
} else {
resolve(null);
}
});
}
walk.js
NN_walkd = async function(req, cnode, pnode, chain, lvl) {
...
if (cnode[1]) {
const sObj = await req.app.get('redis').compSearchK(cnode[1]);
if (sObj) {
int1 = (sObj.TBLNUM==1) ? null : sObj.CLIENTKEY;
(async () => await NN_walkd(req, [sObj.COMPANYKEY,int1], cnode, Array.from(chain), tlvl))()
}
} else {
const sArr = await req.app.get('redis').compSearchM(cnode[0]);
if (sArr.length) {
for (sneaker in sArr) {
(async () => await NN_walkd(req, [sArr[sneaker].COMPANYKEY,sArr[sneaker].CLIENTKEY], cnode, Array.from(chain), tlvl))()
}
} else {
console.log('no more links on this chain: ',cnode);
}
}
}
"doesn't matter if i have async or not here"
compSearchK(ckey) { // doesn't matter if I have a async or not here
return new Promise(async (resolve) => {
const sckey = await this._companyRepo.search()
.where('COMPANYKEY').equals(ckey)
.return.all();
if (sckey.length) {
const ttrr = await this._companyRepo.fetch(sckey[0].entityId);
resolve(ttrr.toJSON());
} else
resolve(null);
});
}
Of course it doesn't matter, because you're not using await inside of compSearchK!
You are using the explicit promise contructor anti-pattern. You should avoid it as it demonstrates lack of understanding. Here is compSearchK rewritten without the anti-pattern -
async compSearchK(ckey) {
// await key
const sckey =
await this._companyRepo.search()
.where('COMPANYKEY').equals(ckey)
.return.all();
// return null if key is not found
if (sckey.length == 0) return null;
// otherwise get ttrr
const ttrr = await this._companyRepo.fetch(sckey[0].entityId);
// return ttrr as json
return ttrr.toJSON();
}

Cannot get the result of an async function in controller

This is my controller:
const rssService = require('../services/rss.service');
async function parser() {
const result = await rssService.rssParser('someurl');
return result;
};
const parse = async function (req, res) {
const p = new Promise((resolve, reject) => {
const t = parser();
if (t === undefined) {
resolve(t);
} else {
// eslint-disable-next-line prefer-promise-reject-errors
reject('something bad happened');
}
});
p.then((result) => res.send(result)).catch((message) => console.log(`ERROR ${message}`));
};
module.exports = {
parse,
};
in the function : parser() above, I am trying to call my rss.service.js file which I have the logic. This file is a rss parser which tries to parse the feed and do some calculations (which needs promises and async) and then return the json object.
Here is how my rss.service look :
const rssParser = async function parseRssFeed(url) {
const parser = new Parser();
const appRoot = process.env.PWD;
const downloadDir = `${appRoot}/downloads/`;
if (!fs.existsSync(downloadDir)) {
fs.mkdirSync(downloadDir);
}
try {
const feed = await parser.parseURL('someurl');
const processedFeedItems = await Promise.all(feed.items.map(async (currentItem) => {
const {
fileUrl,
downloadPath,
} = await downloadFile(currentItem);
const hashValue = calculateHash(downloadPath)
return {
title: currentItem.title,
hash: hashValue,
url: mp3FileUrl,
};
}));
return (JSON.stringify(processedFeedItems));
} catch (error) {
console.error(error);
return 'error';
}
};
when I debug my code I can verify that Json object has been created with correct data, but the result does not return to the callee(controller).
I'll go in a little deeper since you mentioned you're new:
const rssService = require('../services/rss.service');
// This is an async function (always returns a promise)
async function parser() {
const result = await rssService.rssParser('someurl');
return result;
};
const parse = async function (req, res, next) {
// In await/async, you should use try/catch/throw instead of .then and .catch
// It does the same thing, but it's the preferred syntax and is "easier" to read IMO
// Out in "the community" people will complain if you mix await/async with promises like that
try {
// Adding await to ensure left-assign works.
// This is necessary because parser is an async function (so returns a promise)
const result = await parser();
// here, since you used `await` you get the value instead of the promise
if (result === undefined) throw new Error('something bad happened')
return res.send(result)
} catch (error) {
console.log(`ERROR ${error.message}`;
// Do you need to do anything else with this error? I assume something like:
return next(error);
}
};
module.exports = {
parse,
};
In a fast look, It seems you have forgot to wait for resolve the parser promise.
...
const p = new Promise(async(resolve, reject) => {
const t = await parser();
...

How can I get the completed value of an async function when calling a function from index.js?

I'm new to node and I'm trying to create a function to make API requests with pagination. The function successfully gives me the desired output, but I'm confused as to how I can use the .then() function in index.js as it's async. If I use await in my index.js then it throws an error. I hoping to get some guidance as to how I can fix this, and how I can understand async/await better.
//hs-api.js
const request = require('request-promise');
const settings = require('./settings');
var all = []
let getReq = async (url) => {
var options = {
'method': 'GET',
'url': url,
'headers': {
'Content-Type': 'application/json'
}
}
let results = await request(options, function async (error, response) {
if (error) {
reject(error)
} else {
res = JSON.parse(response.body)
}
})
all = all.concat(res.results)
if(res.hasOwnProperty("paging")) {
await getReq(`${res.paging.next.link}&apikey=${settings.api_key}`)
} else {
console.log(all)
return all
}
}
Here is where I call the function
//index.js
let apiResponse = api.getReq(`https://apiexample.com/?apikey=${settings.api_key}`)
console.log(apiResponse)
As of today (Node.js 14), you need to wrap it in a function.
Something like this
(async () => {
let apiResponse = await api.getReq(`https://apiexample.com/?apikey=${settings.api_key}`)
console.log(apiResponse)
})()
There is a proposal for ES for top-level await which will make it possible to run
let apiResponse = await api.getReq(`https://apiexample.com/?apikey=${settings.api_key}`)
console.log(apiResponse)
without wrapping it in an async function.
Node.js (since version 10) has an experimental support for this feature. You need to run it with --experimental-repl-await flag
You need to wrap it in async function in index.js.
// index.js
async someFn() {
let apiResponse = await api.getReq(`https://apiexample.com/?apikey=${settings.api_key}`)
console.log(apiResponse)
}
// call
someFn();
or use '.then'
api.getReq(`https://apiexample.com/?apikey=${settings.api_key}`)
.then(apiResponse => {
console.log(apiResponse);
})
.catch(console.log);
UPD:
//hs-api.js
const results = await request(options);
// don`t sure, that it is correct error indicator for your library
if(result.error) {
throw new Error(// describe error here);
}
const res = JSON.parse(response.body);
all = all.concat(res.results);
if(res.hasOwnProperty("paging")) {
return await getReq(`${res.paging.next.link}&apikey=${settings.api_key}`)
} else {
console.log(all);
return all;
}

Error: await is only valid in async function when function is already within an async function

Goal: Get a list of files from my directory; get the SHA256 for each of those files
Error: await is only valid in async function
I'm not sure why that is the case since my function is already wrapped inside an async function.. any help is appreciated!
const hasha = require('hasha');
const getFiles = () => {
fs.readdir('PATH_TO_FILE', (err, files) => {
files.forEach(i => {
return i;
});
});
}
(async () => {
const getAllFiles = getFiles()
getAllFiles.forEach( i => {
const hash = await hasha.fromFile(i, {algorithm: 'sha256'});
return console.log(hash);
})
});
Your await isn't inside an async function because it's inside the .forEach() callback which is not declared async.
You really need to rethink how you approach this because getFiles() isn't even returning anything. Keep in mind that returning from a callback just returns from that callback, not from the parent function.
Here's what I would suggest:
const fsp = require('fs').promises;
const hasha = require('hasha');
async function getAllFiles() {
let files = await fsp.readdir('PATH_TO_FILE');
for (let file of files) {
const hash = await hasha.fromFile(i, {algorithm: 'sha256'});
console.log(hash);
}
}
getAllFiles().then(() => {
console.log("all done");
}).catch(err => {
console.log(err);
});
In this new implementation:
Use const fsp = require('fs').promises to get the promises interface for the fs module.
Use await fsp.readdir() to read the files using promises
Use a for/of loop so we can properly sequence our asynchronous operations with await.
Call the function and monitor both completion and error.

Async/await with Express returns Promise { <pending> }

I'm trying to use async/await function following some tutorials but I don't know why always returns
Promise { <pending> }
Here is my code:
function doubleAfter2Seconds(x) {
return new Promise(resolve => {
setTimeout(() => {
console.log(x);
resolve(x * 2);
}, 2000);
});
}
async function get_media_async (media_id) {
const a = await doubleAfter2Seconds(10);
return a;
}
exports.get_media = function(media_id){
var media_url = get_media_async(media_id);
return media_url;
};
Any help would be appreciated!
You need to do async/await on get_media method as well. It is because you are calling an async function get_media_async from within this method thus this needs to be async as well.
exports.get_media = async function(media_id){
var media_url = await get_media_async(media_id);
return media_url;
};

Resources