I am currently delving into the intricacies of the callback mechanism in javascript, particularly typescript. If I have a function that expects a callback as an input argument, do I need to explicitly use a return statement to connect it with the actual callback implementation from the calling code? Or can I simply refer to the "callback" parameter in the code being called and have it automatically link up with the callback code in the calling code?
Here are some code samples in typescript:
// Hooking up the callback code using a return statement
clear(collectionName: string, callback: any) {
this.getConnection((err, db) => {
if (!db)
return callback(err, null);
db.collection(collectionName).remove();
});
return callback();
}
// Hooking up the callback code by referencing the reserved callback keyword for automatic connection with the calling code
clear(collectionName: string, callback: any) {
this.getConnection((err, db) => {
if (!db)
return callback(err, null);
db.collection(collectionName).remove({}, callback);
});
}