简体   繁体   English

Node.js - 回调参数

[英]Node.js - callback parameters

I'm currently learning about callbacks in Node and JavaScript in general and am getting confused by the following: 我目前正在学习Node和JavaScript中的回调,我对此感到困惑:

var request = require('request');

request('http://www.google.com', function (error, response, body) {
 if (!error && response.statusCode == 200) {
console.log(body) // Show the HTML for the Google homepage. 
  }
})

My question is this: How does the request function know what each parameter/argument is in the callback? 我的问题是:请求函数如何知道回调中每个参数/参数是什么? Because I could effectively call the function callback with two parameters and skip out the error? 因为我可以用两个参数有效地调用函数回调并跳过错误? How would the function know that the first parameter passed was the response and not the error, for instance? 例如,函数如何知道传递的第一个参数是响应而不是错误?

Does it do checks on the types of each at runtime or? 它是否在运行时检查每种类型? Thanks. 谢谢。

The programmer who wrote the request API decides the order of the parameters sent to the callback. 编写请求API的程序员决定发送给回调的参数的顺序。 There is no type checking at runtime. 在运行时没有类型检查。 If you were to supply only two arguments in your callback signature, eg 如果你只在回调签名中提供两个参数,例如

function(response, body) {}

you would still be receiving the error object and the response -- only in this case, the variable named "response" would hold the error object, and the variable named "body" would hold the response. 你仍然会收到错误对象和响应 - 只有在这种情况下,名为“response”的变量将保存错误对象,名为“body”的变量将保存响应。 The input you would be skipping would be the third one you didn't supply, the body. 你要跳过的输入将是你没有提供的第三个,身体。 The names of your parameters make no difference, it is their position in the function signature that determines what value they will be assigned. 参数的名称没有区别,它们在函数签名中的位置决定了它们将被赋值的值。

By convention, in javascript callbacks, the first parameter of the callback normally represents the error object. 按照惯例,在javascript回调中,回调的第一个参数通常表示错误对象。 It is not always written this way, but it is a best practice. 它并不总是以这种方式编写,但它是最佳实践。

Possibile implementation of this function could be: Possibile实现这个功能可能是:

function request (url, callback) {
  // doing something

  callback (error, response, body);
}

The function will set the value of error , response and body parameters. 该函数将设置errorresponsebody参数的值。 I don't know how it's is implemented, but suppose that you pass a number for the url . 我不知道它是如何实现的,但是假设您为url传递了一个数字。 A possibile implementation could be: 可能的实现可能是:

function request (url, callback) {
  if (typeof url === 'number') {
    // Suppose that error is an instance of Error
    callback (new Error ('Url must be a string'), null, null);
  }
  // other stuff...
}

When you call the request() function, and provide a function as an argument, it calls that function internally (inside the module) and passes in it's own data as the arguments (which you then use to do various things). 当你调用request()函数并提供一个函数作为参数时,它会在内部(模块内部)调用该函数,并将它自己的数据作为参数传入(然后用它来做各种事情)。 The data it passes in is data it generated itself, it's not data you're creating. 它传入的数据是它自己生成的数据,它不是您正在创建的数据。

That is a callback . 这是一个回调

There is a quetsion I answered a little while ago that goes more indepth as to why these modules are structured this way, see: npm's guidelines on callback errors 不久之前我回答了一个问题,更深入地了解为什么这些模块的结构是这样的,请参阅: npm关于回调错误的指导原则

You are using a specific module my friend and callbacks are controlled in this case directly from it. 您正在使用特定模块我的朋友和回调在这种情况下直接从它控制。

How the callback works for 'Request' module is matter of research. 回调如何适用于“请求”模块是研究的问题。 Check their documentation. 检查他们的文档。

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

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