Where does done() come from in Mongoose/Node

Hey all,

I’m trying to figure out where the callback function done() comes from. For example, is it something I have to pass to the task (saving, finding, etc) or is it created somewhere else?

If it is created somewhere else where should I be using it in my code? In the curriculum it is used as assigned to a variable, but I don’t know if that code works on its own or the test suite sends it and calls it?

Thanks

done() generally is a callback function that mongoose/node uses to know when you’re done with your function. You may ask, why don’t they just look for my return statement? The thing is that by using a callback you’re allowed to have async code inside your function.

Let me give you an example.

function myFunction(done) {
  setTimeout(() => done(), 200);
}

myFunction(() => console.log('done called after 200ms'));

As you can see, when you call done() inside myFunction is when the console.log is executed. You don’t need to worry about how done is created inside mongoose/node, what matters is that you call it in your function when necessary to tell them you’re done in your function.