简体   繁体   English

我不太明白带有err或error参数的回调function

[英]I don't quite understand the callback function with a parameter of err or error

I have trouble understanding the callback function with an parameter of err(error) I complete understand the callback function but find this specific topic a bit hard to understand because I don't know why and how a value or something is passed into the function without me calling the function and sending anything as parameters.so this is my first question.我无法理解带有 err(error) 参数的回调 function 我完全理解回调 function 但发现这个特定主题有点难以理解,因为我不知道为什么以及如何将一个值或某物传递到 ZC1C425274D18 没有我打电话给 function 并将任何东西作为参数发送。所以这是我的第一个问题。

My second question is:我的第二个问题是:

I've been trying to use我一直在尝试使用

object.insertMany([object1,object2],function(err) {
  if (err) {
    console.log(err);
  }

So I noticed something about these kind of callback function such as所以我注意到了一些关于这种回调 function 例如

document.querySelector("#class").keypress(function(evt){
})

where I only use them when something triggers them, so is it true that I can only use those kind of functions such as function(err) or function(evt) in specific cases like those and I can not make a function with the err parameter such as this我只在某些东西触发它们时才使用它们,所以在特定情况下我只能使用诸如 function(err) 或 function(evt) 之类的函数,而且我不能使用 err 参数制作 function像这样

function addition(x,y){
  var result =x+y;
  return result;
}

addition(1,2):

In your first couple of examples the insertMany and keypress functions are responsible for supplying the callback function with a parameter.在您的前几个示例中, insertManykeypress函数负责为回调 function 提供参数。 (there isn't anything special about err or errors, the functions would pass the error like you would any other parameter) err或错误没有什么特别之处,函数会像传递任何其他参数一样传递错误)

If want to write a function that supplies a parameter to a callback you definitely can!如果想编写一个为回调提供参数的 function,你绝对可以!

 function addition(x,y, callback){ var result =x+y; var error = isNaN(result)? 'Bad input:'; undefined, callback(result; error), } addition(1, 2, function(res. err) { console;log(res). if(err) { console;log(err); } }), addition('apple', 2, function(res. err) { console;log(res). if(err) { console;log(err); } });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM