Done(null, result) function Missing from documentation

So, In a MongoDB challenge I am introduced with a function, called done, done(null, result); but the thing is that I can’t find it in any documentation, nor nodejs’s, express’s, MongoDB’s etc. I could only find it related to jQuery and Passport, which should not be relevant to this challenge. Can any of you help me find the official documentation on this mysterious done function?

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36.

Challenge: Create a Model

Link to the challenge:

const someFunc = function(flarb) {
  //... do something (risky) ...
  if (error) return flarb(error);
  flarb(null, result);
};

This is exactly the same. done is just what the callback function is called by convention.

The above is a function that takes in argument. That argument has to be a function. What it’s called isn’t important, but by convention it’s always called done because it is supposed to be called when the functionality is done

1 Like

Thanks for your reply. Clearly I’m missing some key concept. Let me ask you a bit more, what is the context in which this callback function ins interpreted? In other words, what defines what flarb/done will do with the null and result arguments? Is it node, mongoose or something else? Hope the question makes sense.

Hello there,

If you look within server.js, all of the functions you are completing are being imported.

Take createAndSavePerson as an example:

  • It is defined as a function, which takes a callback function (named done)
  • In server.js (line ~78), it is imorted as createPerson:
var createPerson = require('./myApp.js').createAndSavePerson;
  • Then, attached as part of a route callback:
router.get('/create-and-save-person', function(req, res, next) {
  • Then, used, by defining a function inside it like so:
  // in case of incorrect function use wait timeout then respond
  var t = setTimeout(() => { next({message: 'timeout'}) }, timeout);
  createPerson(function(err, data) {
    clearTimeout(t);
    if(err) { return (next(err)); }
    if(!data) {
      console.log('Missing `done()` argument');
      return next({message: 'Missing callback argument'});
    }
     Person.findById(data._id, function(err, pers) {
       if(err) { return (next(err)); }
       res.json(pers);
       pers.remove();
     });
  });
});

That is a bit much code to simply digest, so, I would split it like this:

function done(err, data) {
    clearTimeout(t);
    if(err) { return (next(err)); }
    if(!data) {
      console.log('Missing `done()` argument');
      return next({message: 'Missing callback argument'});
    }
     Person.findById(data._id, function(err, pers) {
       if(err) { return (next(err)); }
       res.json(pers);
       pers.remove();
     });
}

// Call the function YOU created, passing 'done' (defined above)
createPerson(done);

Hope this clarifies

1 Like

Yes I does. the way you use Node is entirely based on callbacks, it’s how code for it is written. It’s called continuation passing style.

You want something (b) to happen once something else (a) has finished. a and b are functions. So how do you make sure b happens after a? Well, you just pass function b to a as an argument, and call it in a. And then you just can just build big chains of these functions, as long as you stick to always passing them along.

The example is just an example to show you this. done is just the function you want to execute when the example function has done what it’s supposed to do.

Node standard is to have two callbacks, error and done, in that order. Because the convention is for this, then everything basically works the same way. Mongoose is a library for Node, so uses same technique, as does almost every other Node library.

3 Likes

Thank you so much, it is really clarifying!

Appreciate it @DanCouper !!!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.