Search code examples
node.jsdocumentation

How do you read API documentation in Node.js?


I am trying to read the documentation at https://github.com/mikeal/bent.

I don't know how to read async request(url[, body=null, headers={}]). Specifically, what does the , represent?

I have seen an answer to this question before, and I have searched for it, but I could not find it. Marking this as a duplicate of that would be fantastic.


Solution

  • Here's a step by step for this example:

    async request(url[, body=null, headers={}])
    
    1. The async means it's an async function that always returns a promise.
    2. url is the first argument and is required.
    3. The [, args here ] means that the arguments inside the brackets are optional.
    4. The comma in [, args here] means that if you include these arguments, then a comma is required to separate each argument from the previous one.
    5. body=null means that if you don't pass the body argument, the default value is null
    6. headers={} means that if you don't pass the headers argument, the default value is an empty object.

    So, you can call it like any of these:

    request(myUrl).then(...).catch(...)
    request(myUrl, myBody).then(...).catch(...)
    request(myUrl, myBody, myHeaders).then(...).catch(...)