• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

help to understand callback functions in Javascript

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I am trying to learn Node.js through a hands-on course which said that the only prerequisite was basic knowledge of JavaScript. Though i've got a reasonable idea of Routes, Controllers, complex areas of javascript still seem like a mystery.


For example in the below piece of code, can somebody explain why "callback" is written inside the function parenthesis? also, I assume that the results returned by the function are passed into a variable named genre. Is the ':' following genre a substitute to '='?

 
Saloon Keeper
Posts: 12251
259
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not familiar with the library you're using, but it looks like async.parallel executes functions you provide to it in parallel, and then saves the results of those parallel function calls in a results object.

In this particular case, you provide it two functions to call in parallel: genre and genre_books. When those functions have performed their calculations, async.parallel still has to perform some logic to put the results in the object. It does this by passing a callback function to the functions you want to perform in parallel. You call the callback function on the results, so async.parallel can perform the final steps.

After the callback has been executed, the async.parallel passes the object containing the results that were passed to the callbacks to the function at the bottom, where you can perform your final logic on the results.

So, why don't the functions genre and genre_books just return their results, instead of passing them to the callback? This is just a different style of programming, called continuation-passing style.
 
sinatra roger
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your help. This makes it easier to understand. I am still trying to get accustomed to this type of programming where functions are nested inside functions and callback techniques etc. Though I have been able to follow most of the tutorial (https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs), I think a good knowledge on these Javascript programming techniques would have been really helpful. Can you point out on a good book which explains these techniques specific to JS?
 
Stephan van Hulst
Saloon Keeper
Posts: 12251
259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, most of what I know is from programming in other functional languages, like Haskell.
 
Marshal
Posts: 67446
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to really know how JS works, that's why we wrote Secrets of the JavaScript Ninja. It will tell you how JavaScript works under covers so you can completely understand how functions, callbacks, closures and other important concepts fit into the big picture.

 
    Bookmark Topic Watch Topic
  • New Topic