Why the result is undefined when returned when when logged, logs the result. | Sololearn: Learn to code for FREE!

+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

13 Answers

New Answer

+4

(JAM) ‎جوردن آهو ماولی‎ Found solution, readdir is async so you need to use readdirSync or write callback function https://code.sololearn.com/cQs1w9ZWwLmp/?ref=app https://stackoverflow.com/questions/40629186/fs-readdir-returns-undefined-when-called-inside-a-function

+4

visph hello there 😁. Could you please help me out?

+4

Because folder doesn't exist.

+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.

+3

A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ hmmm okay 👌 😊. Thanks for your effort, bro 😅

+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.

+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)

+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.

+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.

+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.

+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.

+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.

+1

(JAM) ‎جوردن آهو ماولی‎ No folder doesn't exist. It is showing error on Mobile. Print err and see log console.log(err)