How is this method written so that the callback parameters are valid?
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
add a comment |
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
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
add a comment |
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
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
javascript node.js
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 15 '18 at 21:03
answered Nov 15 '18 at 20:50
jfriend00jfriend00
439k55576621
439k55576621
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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