0

I was trying to learn about async/promises/callbacks/js modules/etc... However, when I was trying to run a simple module which only required a file, and ran a function that only returned the parameter back, node was not outputting the result correctly. By this I mean that it would only sometimes return the value, and sometimes not.

I am pretty new so I could be wrong, but from what I understand this is async in node(?). But while researching I was only finding functions which do things like load, wait, timeout, etc. in lists for things that use async. But it seems that everything I run in node, no matter how simple runs with async and will not consistently output values? Here is an example of what I ran and recieved in the node console:

> function test(input){
... return input;
... }
> test('a')

> test('a')
'a'
>
> test(5)

> test(5)
5
>

Like I said, I was working on learning about a few things, but it started by looking into modules, so I was also wondering what a common 'pattern'(?) for handling this is, when exporting modules. Thanks.

Edit:

I'm not sure if I made it very clear, but for the most part I was really just wondering why a function as simple as this wouldn't be working, when the only functions I have seen people talk about async with are functions that are obviously waiting, in a sense. Not simple functions like this. I havent tried putting a callback with this yet, but I assume that works. I just didn't think you needed them for functions as simple as this?

Gage Hafl
  • 21
  • 1
  • If it where an async function it would return the value eventually if you don't kill the process. The test function you describe should return immediatly the result, if it doesnt there is an unrelated problem. – Aramil Rey Mar 29 '17 at 18:20
  • required a file? If you mean you are using readFile from nodejs file system this will be Asynchronous because there might be a lock on the file. This way your application doesn't get held up. – Steven Johnston Mar 29 '17 at 18:35
  • [The default is synchronous](http://stackoverflow.com/q/2035645/1048572). Only some functions return synchronously but also start of some asynchronous processing - see [Are all javascript callbacks asynchronous? If not, how do I know which are?](http://stackoverflow.com/q/19083357/1048572) and [Are all Node.js callback functions asynchronous?](http://stackoverflow.com/q/21884258/1048572) for those – Bergi Mar 29 '17 at 19:05
  • Your example output makes no sense. There's no reason why it wouldn't print the same result for two equivalent calls. This has nothing to do with asynchrony. – Bergi Mar 29 '17 at 19:07
  • @steven Johnston, I guess i made it confusing, but at first I was doing stuff with modules and require, but then I ran only the code shown here and got that output – Gage Hafl Mar 30 '17 at 05:36
  • @AramilRey like I said I am really new to this stuff, and the only explanation I could think of was async related, so I don't know what is happening – Gage Hafl Mar 30 '17 at 05:36

3 Answers3

0

This is how the same input in the Node REPL works for me:

> function test(input){
... return input;
... }
undefined
> test('a')
'a'
> test('a')
'a'
> test(5)
5
> test(5)
5
>

The test() function is not asynchronous in your example. It returns the value synchronously. The REPL session that you included doesn't seem rea, though, as your function definition should return undefined like in my example.

To answer your question: Not all of Node functions have to be asynchronous. Every function that simply return a value is synchronous - like e.g. Math.sin() etc.

For a better understanding of all this, take a look at this answer that shows examples of both synchronous and asynchronous functions, explaining the order of execution step by step:

rsp
  • 107,747
  • 29
  • 201
  • 177
  • sorry i messed up when I copied it over, but node does show undefined when I define a function. also I have been using git-bash and conemu, running node to test these out? I wouldn't have thought that made a difference. Does it? – Gage Hafl Mar 29 '17 at 18:33
  • And thats also what I thought, exactly what you said. That is why I am confused on why it will not output values consistently synchronously when the function clearly is. – Gage Hafl Mar 29 '17 at 18:34
  • @GageHafl This is very strange. When you have such a function it should always return the same value. The only reason it doesn't would be that the REPL itself is somehow not working correctly. If you put that function in a file and add `console.log(test('a'));` I expect it will always print `'a'` on every execution. If it does then it means that your console is acting strangely. – rsp Mar 29 '17 at 18:37
  • yes if forgot to mention that console logs work 100% of the time. And both of the node consoles are doing exactly the same things, with non-definite output and always working console logs – Gage Hafl Mar 29 '17 at 18:40
0

Use something like this

function test(input,callback){
 return callback(input);
}

test('a',function(s){console.log(s)})
Vinayk93
  • 353
  • 1
  • 6
-1

Based on your question, the function you are using is not an async function and doesn't model async operations. It blocks until a result is returned.

Don't be confused by the results returned by your cli; it's normal to have that at times. Async operations however are used to abstract long running operations in node such as database requests, file i/o requests, online info requests etc that returns info at a later time.

A more appropriate approach is to use a function with callbacks, use the es7 async and await or use event emitters. Those are the only ways to handle async operations in node.

You might want to read beginning node js

symcbean
  • 47,736
  • 6
  • 59
  • 94
Gad
  • 11
  • 2
  • 2
    Your answer is very hard to read, try rewriting it without all the extra `.` characters. Additionally, try giving some examples of what you mean, rather than just a text description. This isn't a very useful answer without more content. – Adam Mar 29 '17 at 18:46