简体   繁体   中英

How to check the the arguments for certain callback function in Node.js?

 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. } }) 

For the above example, how suppose I know the request() function will have error, response, body to the callback function? I tried google it but can't find a clear answer. I can remember this one, but how about new ones without anyone telling me what are the arguments and what's the order? Any way to check?

Thanks!

您可以使用此控制台所有参数。

function callback() { console.log(arguments); }

  • By reading documentation, which is hopefully up-to-date (ideally the first place to look)
  • By reading source code, which is certainly correct but possibly not easily understandable
  • By trying it out and looking what comes back, and hoping to understand what's what:

     request('http://www.google.com', function (...args) { console.log(args); }); 

As a note, NodeJS use Error First Callbacks which means that the first argument in the callback is the error paramater. And the second parameter is always the response from the operations like API calls or database calls. While making third party API calls from the NodeJS code, they provide an extra parameter in the callback, which is the third parameter to give a detail insight of the operation you perform on that API.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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