+ 6

Why the result is undefined when returned when when logged, logs the result.

Nodejs file system module. readdir async method https://code.sololearn.com/ca6tk26Ez72F/?ref=app

14th Nov 2021, 4:26 AM
جوردن آهو ماولی
جوردن آهو ماولی - avatar
13 Respuestas
+ 4
visph hello there 😁. Could you please help me out?
14th Nov 2021, 4:27 AM
جوردن آهو ماولی
جوردن آهو ماولی - avatar
+ 4
Because folder doesn't exist.
14th Nov 2021, 4:39 AM
A͢J
A͢J - avatar
+ 4
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ when you can't get the real folder on Sololearn as the folder in on my pc. So when you're free, you can test on your pc.
14th Nov 2021, 1:30 PM
جوردن آهو ماولی
جوردن آهو ماولی - avatar
+ 3
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ hmmm okay 👌 😊. Thanks for your effort, bro 😅
14th Nov 2021, 1:48 PM
جوردن آهو ماولی
جوردن آهو ماولی - avatar
+ 2
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ thanks for taking your time to reply. The folder does exist. And the results were logged when I used console.log but it didn't return it. You may run the code on your pc and create the respective files.
14th Nov 2021, 9:56 AM
جوردن آهو ماولی
جوردن آهو ماولی - avatar
+ 2
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ yeah it's async but it still logged when console.log was used. Also I used the callback as you can see in the code. Console.log should also be undefined since the data was not yet available (done processing)
14th Nov 2021, 12:26 PM
جوردن آهو ماولی
جوردن آهو ماولی - avatar
+ 2
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ I'm not interested in fixing it. I want to know why it was logged and returning it gave undefined. Also, I don't want to use the synchronous version which is readdirSync because it blocks the event loop which is not good.
14th Nov 2021, 12:28 PM
جوردن آهو ماولی
جوردن آهو ماولی - avatar
+ 2
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ i tried awaiting the results by encapsulating it into an anonymous async function but still gave the same results.
14th Nov 2021, 12:30 PM
جوردن آهو ماولی
جوردن آهو ماولی - avatar
+ 2
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ i read your code. Thanks. There's one problem. What if we don't know how long the promise will take before being resolved? Though promises have higher priority than setTimeout in the event loop and thus are processed first, after the promise is popped off the stack and being processed, the setTimeout gets into the call stack and if the time for the setTimeout is lesser than the promise, then it will return undefined again or promise pending. I tried this by passing 0 milliseconds to the setTimeout and that should still be called after the promise but it returned undefined again.
14th Nov 2021, 1:07 PM
جوردن آهو ماولی
جوردن آهو ماولی - avatar
+ 2
(JAM) ‎جوردن آهو ماولی‎ I am not using PC so I can't tell why but as on mobile we can see folder doens't exist so on the basis of that it is returning undefined.
14th Nov 2021, 1:17 PM
A͢J
A͢J - avatar
+ 2
(JAM) ‎جوردن آهو ماولی‎ As readdir is asynchronous and folder doesn't exist so we are getting fast response before getting the error. Which result is undefined with error. As you can see when I didn't use sync method we got undefined with error but when I did sync then we just got error without undefined. So try once with readdirSync method.
14th Nov 2021, 1:42 PM
A͢J
A͢J - avatar
+ 1
(JAM) ‎جوردن آهو ماولی‎ No folder doesn't exist. It is showing error on Mobile. Print err and see log console.log(err)
14th Nov 2021, 10:21 AM
A͢J
A͢J - avatar