Thursday 30 November 2017

confusion about functions exiting without return statement

I'm relatively new to nodejs and asynchronous programming in general. I have a section of code that is composed of a function that returns a promise followed by 2 functions connected by "then":function func_with_a_promise() { return new Promise (function (fulfill, reject){ fulfill(); }) } func_with_a_promise().then( function(){ console.log("There is no return statement in this function") } ).then( function(){ console.log("But we still arrive here...") } ) The output of this code is:There is no return statement in this function But we still arrive here... I would expect that the output is just "There is no return statement in this function", and the final function would never be reached because the second function doesn't return anything. But in fact both the final two functions are reached and output to the console.I must be misunderstanding something, please help!edit: ok after posting this I did a bit of digging around. It seems that javascript functions don't need a return statement. If you don't return anything, then javascript automatically returns "undefined". Is this correct?

Submitted November 30, 2017 at 05:01PM by thenewstampede

No comments:

Post a Comment