How is this method written so that the callback parameters are valid?












-2















This is a method from Twitter package from npm.



  T.get('search/tweets', params, function(err, data, response) {
if(!err){
// This is where the magic will happen
} else {
console.log(err);
}
})


I know that normally you'd write a method with callback like this



    T.prototype.get = function(url, options, callback) {
//code here
callback();

}


My question is how are the "err", "data", and "response" values are passed to callback function. Is it by doing the following?



callback(error, returnedTweets, reponseObject);









share|improve this question


















  • 5





    The answer to the question you have asked at the bottom of your post is: yes that is exactly what is happening.

    – mhodges
    Nov 15 '18 at 20:43













  • Have you tried looking into the source of that package? Yes, you should find a line just like that.

    – Bergi
    Nov 15 '18 at 20:55
















-2















This is a method from Twitter package from npm.



  T.get('search/tweets', params, function(err, data, response) {
if(!err){
// This is where the magic will happen
} else {
console.log(err);
}
})


I know that normally you'd write a method with callback like this



    T.prototype.get = function(url, options, callback) {
//code here
callback();

}


My question is how are the "err", "data", and "response" values are passed to callback function. Is it by doing the following?



callback(error, returnedTweets, reponseObject);









share|improve this question


















  • 5





    The answer to the question you have asked at the bottom of your post is: yes that is exactly what is happening.

    – mhodges
    Nov 15 '18 at 20:43













  • Have you tried looking into the source of that package? Yes, you should find a line just like that.

    – Bergi
    Nov 15 '18 at 20:55














-2












-2








-2








This is a method from Twitter package from npm.



  T.get('search/tweets', params, function(err, data, response) {
if(!err){
// This is where the magic will happen
} else {
console.log(err);
}
})


I know that normally you'd write a method with callback like this



    T.prototype.get = function(url, options, callback) {
//code here
callback();

}


My question is how are the "err", "data", and "response" values are passed to callback function. Is it by doing the following?



callback(error, returnedTweets, reponseObject);









share|improve this question














This is a method from Twitter package from npm.



  T.get('search/tweets', params, function(err, data, response) {
if(!err){
// This is where the magic will happen
} else {
console.log(err);
}
})


I know that normally you'd write a method with callback like this



    T.prototype.get = function(url, options, callback) {
//code here
callback();

}


My question is how are the "err", "data", and "response" values are passed to callback function. Is it by doing the following?



callback(error, returnedTweets, reponseObject);






javascript node.js






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 20:40









kshatriiyakshatriiya

749




749








  • 5





    The answer to the question you have asked at the bottom of your post is: yes that is exactly what is happening.

    – mhodges
    Nov 15 '18 at 20:43













  • Have you tried looking into the source of that package? Yes, you should find a line just like that.

    – Bergi
    Nov 15 '18 at 20:55














  • 5





    The answer to the question you have asked at the bottom of your post is: yes that is exactly what is happening.

    – mhodges
    Nov 15 '18 at 20:43













  • Have you tried looking into the source of that package? Yes, you should find a line just like that.

    – Bergi
    Nov 15 '18 at 20:55








5




5





The answer to the question you have asked at the bottom of your post is: yes that is exactly what is happening.

– mhodges
Nov 15 '18 at 20:43







The answer to the question you have asked at the bottom of your post is: yes that is exactly what is happening.

– mhodges
Nov 15 '18 at 20:43















Have you tried looking into the source of that package? Yes, you should find a line just like that.

– Bergi
Nov 15 '18 at 20:55





Have you tried looking into the source of that package? Yes, you should find a line just like that.

– Bergi
Nov 15 '18 at 20:55












1 Answer
1






active

oldest

votes


















2














Yes, the parameters are sent to the callback by the caller of the callback which in your example would be the internal implementation of T.get(). So, if you get three parameters on the callback like this function(err, data, response) ..., then it's because that's how the callback was called with three arguments that are of the appropriate type to match that:



T.prototype.get = function(url, options, callback) {
//code here
callback(err, data, response);

}




It does not matter what the arguments that are passed are named locally. So, internal to the T.get() implementation, those arguments may have different names:



T.prototype.get = function(url, options, callback) {
//code here
callback(error, returnedTweets, reponseObject);
}


It is the values of those variables that are passed to the callback. The name they have inside the implementation is meaningless to the callback itself.





The name you give them when you declare the callback in this code is also irrelevant:



T.get('search/tweets', params, function(err, data, response) {
// callback implementation here
});


You can pick any names for the callback arguments you want. The callback arguments are passed in a specific order so whatever you name the first argument in your callback declaration is what will have the value of the first argument that was passed when the callback was called. Arguments in Javascript are by order/position, not by name. For example, you could have named them like this:



T.get('search/tweets', params, function(arg1, arg2, arg3) {
// callback implementation here
});


And, the code would still work properly, though it would be a lot less readable because one would have to just know that arg1 is the error value and arg2 is the data and so on. So, most developers pick meaningful names that help describe what is expected in that argument. But, the name you pick for a callback argument is entirely up to you.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327581%2fhow-is-this-method-written-so-that-the-callback-parameters-are-valid%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Yes, the parameters are sent to the callback by the caller of the callback which in your example would be the internal implementation of T.get(). So, if you get three parameters on the callback like this function(err, data, response) ..., then it's because that's how the callback was called with three arguments that are of the appropriate type to match that:



    T.prototype.get = function(url, options, callback) {
    //code here
    callback(err, data, response);

    }




    It does not matter what the arguments that are passed are named locally. So, internal to the T.get() implementation, those arguments may have different names:



    T.prototype.get = function(url, options, callback) {
    //code here
    callback(error, returnedTweets, reponseObject);
    }


    It is the values of those variables that are passed to the callback. The name they have inside the implementation is meaningless to the callback itself.





    The name you give them when you declare the callback in this code is also irrelevant:



    T.get('search/tweets', params, function(err, data, response) {
    // callback implementation here
    });


    You can pick any names for the callback arguments you want. The callback arguments are passed in a specific order so whatever you name the first argument in your callback declaration is what will have the value of the first argument that was passed when the callback was called. Arguments in Javascript are by order/position, not by name. For example, you could have named them like this:



    T.get('search/tweets', params, function(arg1, arg2, arg3) {
    // callback implementation here
    });


    And, the code would still work properly, though it would be a lot less readable because one would have to just know that arg1 is the error value and arg2 is the data and so on. So, most developers pick meaningful names that help describe what is expected in that argument. But, the name you pick for a callback argument is entirely up to you.






    share|improve this answer






























      2














      Yes, the parameters are sent to the callback by the caller of the callback which in your example would be the internal implementation of T.get(). So, if you get three parameters on the callback like this function(err, data, response) ..., then it's because that's how the callback was called with three arguments that are of the appropriate type to match that:



      T.prototype.get = function(url, options, callback) {
      //code here
      callback(err, data, response);

      }




      It does not matter what the arguments that are passed are named locally. So, internal to the T.get() implementation, those arguments may have different names:



      T.prototype.get = function(url, options, callback) {
      //code here
      callback(error, returnedTweets, reponseObject);
      }


      It is the values of those variables that are passed to the callback. The name they have inside the implementation is meaningless to the callback itself.





      The name you give them when you declare the callback in this code is also irrelevant:



      T.get('search/tweets', params, function(err, data, response) {
      // callback implementation here
      });


      You can pick any names for the callback arguments you want. The callback arguments are passed in a specific order so whatever you name the first argument in your callback declaration is what will have the value of the first argument that was passed when the callback was called. Arguments in Javascript are by order/position, not by name. For example, you could have named them like this:



      T.get('search/tweets', params, function(arg1, arg2, arg3) {
      // callback implementation here
      });


      And, the code would still work properly, though it would be a lot less readable because one would have to just know that arg1 is the error value and arg2 is the data and so on. So, most developers pick meaningful names that help describe what is expected in that argument. But, the name you pick for a callback argument is entirely up to you.






      share|improve this answer




























        2












        2








        2







        Yes, the parameters are sent to the callback by the caller of the callback which in your example would be the internal implementation of T.get(). So, if you get three parameters on the callback like this function(err, data, response) ..., then it's because that's how the callback was called with three arguments that are of the appropriate type to match that:



        T.prototype.get = function(url, options, callback) {
        //code here
        callback(err, data, response);

        }




        It does not matter what the arguments that are passed are named locally. So, internal to the T.get() implementation, those arguments may have different names:



        T.prototype.get = function(url, options, callback) {
        //code here
        callback(error, returnedTweets, reponseObject);
        }


        It is the values of those variables that are passed to the callback. The name they have inside the implementation is meaningless to the callback itself.





        The name you give them when you declare the callback in this code is also irrelevant:



        T.get('search/tweets', params, function(err, data, response) {
        // callback implementation here
        });


        You can pick any names for the callback arguments you want. The callback arguments are passed in a specific order so whatever you name the first argument in your callback declaration is what will have the value of the first argument that was passed when the callback was called. Arguments in Javascript are by order/position, not by name. For example, you could have named them like this:



        T.get('search/tweets', params, function(arg1, arg2, arg3) {
        // callback implementation here
        });


        And, the code would still work properly, though it would be a lot less readable because one would have to just know that arg1 is the error value and arg2 is the data and so on. So, most developers pick meaningful names that help describe what is expected in that argument. But, the name you pick for a callback argument is entirely up to you.






        share|improve this answer















        Yes, the parameters are sent to the callback by the caller of the callback which in your example would be the internal implementation of T.get(). So, if you get three parameters on the callback like this function(err, data, response) ..., then it's because that's how the callback was called with three arguments that are of the appropriate type to match that:



        T.prototype.get = function(url, options, callback) {
        //code here
        callback(err, data, response);

        }




        It does not matter what the arguments that are passed are named locally. So, internal to the T.get() implementation, those arguments may have different names:



        T.prototype.get = function(url, options, callback) {
        //code here
        callback(error, returnedTweets, reponseObject);
        }


        It is the values of those variables that are passed to the callback. The name they have inside the implementation is meaningless to the callback itself.





        The name you give them when you declare the callback in this code is also irrelevant:



        T.get('search/tweets', params, function(err, data, response) {
        // callback implementation here
        });


        You can pick any names for the callback arguments you want. The callback arguments are passed in a specific order so whatever you name the first argument in your callback declaration is what will have the value of the first argument that was passed when the callback was called. Arguments in Javascript are by order/position, not by name. For example, you could have named them like this:



        T.get('search/tweets', params, function(arg1, arg2, arg3) {
        // callback implementation here
        });


        And, the code would still work properly, though it would be a lot less readable because one would have to just know that arg1 is the error value and arg2 is the data and so on. So, most developers pick meaningful names that help describe what is expected in that argument. But, the name you pick for a callback argument is entirely up to you.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 15 '18 at 21:03

























        answered Nov 15 '18 at 20:50









        jfriend00jfriend00

        439k55576621




        439k55576621
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327581%2fhow-is-this-method-written-so-that-the-callback-parameters-are-valid%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            List item for chat from Array inside array React Native

            Thiostrepton

            Caerphilly