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.
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
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.
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.