Ken Kwok Ken Kwok - 1 year ago 78
Node.js Question

What does 'cb' mean in loopback?

I am trying to learn loopback but I don't really understand what 'cb' means in function call. I read this In loopback documentaion what does variable 'cb' stands for? and I have basic understanding of callback in nodejs but I just don't understand cb in loopback.
For example,

module.exports = function(Person){

Person.greet = function(msg, cb) {

cb(null, 'Greetings... ' + msg);





accepts: {arg: 'msg', type: 'string'},

returns: {arg: 'greeting', type: 'string'}




What does that cb mean? How can we know it accepts two parameters, null and a string? Hope someone could help.

Answer Source

So you have an Async function Person.greet which you'll call like this:

Person.greet('hello', function(err){

Notice that after 'hello' a second argument was passed and it is actually a function. It can also be defined outside with a name and passed this way:

function callback(err){
Person.greet('hello', callback);

Now it looks exactly how the Person.greet was defined:

Person.greet = function(msg, cb) {
  cb(null, 'Greetings... ' + msg);

The difference here is just that in the definition it uses a different name: cb. It could've used any name since to it cb is just an argument. But generally "cb", "done", or "next" are used as a standard practice.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download