In loopback documentation what does variable 'cb' stands for?

筅森魡賤 提交于 2019-12-11 03:33:08

问题


Look at the loopback code in their documentation http://docs.strongloop.com/display/public/LB/Defining+and+using+roles, what I am trying to understand since start is 'cb'. I understand it is some kind of callback but why it is all round the place? Does it have to do anything with Async.js??


回答1:


It has to do with the way node works asynchronously. It uses an 'event loop' that passes off other i/o functions to a background worker thread. When the background work completes the event loop receives a callback. There's a good discussion of this here: Why is node.js asynchronous?

Node libraries that call on expensive resources follow this model to gain performance.

The callback is a function you pass into the library function which is executed when that function completes its processing. It's often anonymous.

The convention is to have this function accept an error parameter as the first argument and results as subsequent ones. You'll see this pattern everywhere:

lib.somfunc( 'argument', function(err, res){

    if(err)....

}); 

Async.js is something a bit different. It's a library that provides various means for orchestrating asynchronous code that uses callbacks.



来源:https://stackoverflow.com/questions/30936909/in-loopback-documentation-what-does-variable-cb-stands-for

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!