callback function – express MDN tutorial

Issue

As I’m currently learning coding with express/Node in order to evolved (used to C and PHP/MySQL…) I have completed the MDN tutorial on express which very well done and every thing is pretty much straight forward; my personnel project is going almost done, thanks to the Mozilla teaching team.

However, here is a point I still can’t figure out as I’m still not confortable with the use of Callbacks function.
The point of dealing with asynchronous timing of execution I get, but using MongoDB and mongoose in this tutorial, I’ve got that queries can be executed either in two steps or in one go by using directly a callback function like creating an instance of a Schema:

// Create an instance of model SomeModel
var awesome_instance = new SomeModel({ name: 'awesome' });

// Save the new model instance, passing a callback
awesome_instance.save(function (err) {
  if (err) return handleError(err);
  // saved!
});

or

SomeModel.create({ name: 'also_awesome' }, function (err, awesome_instance) {
  if (err) return handleError(err);
  // saved!
});

However, in the JS script given to fill the DB with data, it seems that both syntaxes are used, here is an example:

function authorCreate(first_name, family_name, d_birth, d_death, cb) {
  authordetail = {first_name:first_name , family_name: family_name }
  if (d_birth != false) authordetail.date_of_birth = d_birth
  if (d_death != false) authordetail.date_of_death = d_death
  
  var author = new Author(authordetail);
       
  author.save(function (err) {
    if (err) {
      cb(err, null)
      return
    }
    console.log('New Author: ' + author);
    authors.push(author)
    cb(null, author)
  }  );
}

What troubles me is that

  • "cb" is never defined,
  • the script works the same if I delete all callbacks from arguments "defined" in functions and their respective calling
  • What is the point of cb(null, author): no data need to be return, they are push to the declared array and saved to the DB at the same time.

The full script can be found here: https://raw.githubusercontent.com/hamishwillee/express-locallibrary-tutorial/master/populatedb.js

Thanks anyone who takes time to read and answer me,

Tiago

Solution

Welcome to stack

in Js world , the function can be treated as a variable , the function is a type of variable like String , Number …etc , which may confuse you .. the callback is just a function that has a variable ( argument ) name … now swift language has implemented that pattern as well .

The functions in JS could be sent to other functions as an argument and could be returned as well.

The callback argument sent by the function caller like any other variable sent to the called function but sent as a function …

You could name it anything like cb or whatever … The cb argument is a function which means when you try to use it inside the called function it may need arguments as well like cb(null, author) .. Now imagine the cb is just a variable of type function sent from the caller to the called function.. and the cb itself as a function has to receive arguments .

I know it’s confusing especially if cb is returned as well .

Sometimes callbacks don’t receive any arguments , Again you may call them anything like callback or cb as a convention .. Usually they are the last parameter, as a convention … It just needs some imagination to understand how this confusing parameter goes in and out …

Inside your called function if you didn’t use your callback so removing it will not affect the code as you will not examine errors . BUT in node world callbacks are usually important because it does something after you finish what you’re doing .

i hope that simple illustration clarifies what you’re asked because it has confued me a lot like you .

Answered By – Hossam Maher

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published