what is the callback in loopback (datasource.(automigrate))

≯℡__Kan透↙ 提交于 2020-01-17 05:29:11

问题


Loopback datasource API offers automigrate function with an optional callback.

I see in some examples that the callback gets one parameter (err), but no definition of that.

  • What form does the callback parameter have?
  • Are there other possible parameters?
  • How is this with the other functions?

回答1:


Callbacks are nothing but the function which you passing as a parameter to the other function

Look at this example

function printResult(err,result) {
  if(err) {
    console.log('something went wrong');
  }else{
   console.log(result); 
  }
}

function giveMeDouble(val, cb){
  if(val!=2){
    var err = new Error("value is not 2");
    cb(err);
  }
  cb(null,2*2);
}         
// Passing printResult function as a callback to the giveMeDoubleFunction
giveMeDouble(2,printResult); 

The Other Way of doing the same

giveMeDouble(2,function(err,result){
 if(err) {
   console.log('something went wrong');
  }else{
    console.log(result);
  }
});

Generally in Loopback form of callback is the first parameter is err and the second is the success res if everything went good but you can always have more parameters depend upon the function which you are calling. In your case callback form will be

dataSource.automigrate(model, function(err,result) {
})


来源:https://stackoverflow.com/questions/37216699/what-is-the-callback-in-loopback-datasource-automigrate

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