在回送文档中,变量"cb"代表什么? [英] In loopback documentation what does variable 'cb' stands for?
问题描述
请参阅其文档 http://docs.strongloop.com/display/public/LB/Defining+and+using+roles ,自开始以来,我试图理解的是'cb'.我知道这是某种回调,但是为什么它遍地都是呢?它与Async.js有什么关系吗?
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??
推荐答案
它与节点异步工作的方式有关.它使用一个事件循环",该事件循环将其他I/O函数传递给后台工作线程.后台工作完成后,事件循环将收到回调.这里有一个很好的讨论:为什么node.js是异步的?
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有点不同.这是一个提供各种方法来编排使用回调的异步代码的库.
Async.js is something a bit different. It's a library that provides various means for orchestrating asynchronous code that uses callbacks.
这篇关于在回送文档中,变量"cb"代表什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!