number of parameters in callback

Hi,

I have a main easyhttp.js file as below:
function easyHTTP() {
  this.http = new XMLHttpRequest();
}

easyHTTP.prototype.get = function (url, callback) {
  this.http.open('GET', url, true);

  self = this;
  this.http.onload = function () {
    if (self.http.status === 200) {
      callback(null, self.http.responseText);
    } else {
      callback('Error: ' + self.http.status);
    }
  };
  this.http.send();
}

Open in new window

And I execute it on the following app.js file.

const http = new easyHTTP();

http.get('https://jsonplaceholder.typicode.com/posts',
  (err, posts) => {
    if (err) {
      console.log(err);
    } else {
      console.log(posts);
    }
  })

Open in new window


Now what i don't understand is in app.js when we create a callback function, we pass 2 parameters (err, posts) right?
How come we pass only one argument when we call callback function as below?
callback('Error: ' + self.http.status);

Open in new window


Also, in callback function declaration, we pass 2 parameters and in 'if' statement, we pass only one like if(err).
How this function knows that which one is err and which one is posts when only one parameter is passed.
IsabellAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
Capture-d-e-cran-2019-03-23-a--13.05.png
Julian HansenCommented:
Let's break your code down
Line 5 of first listing
easyHTTP.prototype.get = function (url, callback) {

Open in new window

The get() function expects two parameters: url and a callback function
Let's look at your second listing - this is using arrow functions - it might be easier to see what is going on if we rewrite it to look like this
http.get('https://jsonplaceholder.typicode.com/posts', function  (err, posts) {
    if (err) {
      console.log(err);
    } else {
      console.log(posts);
    }
  })

Open in new window

From the above we can see that the callback takes two parameters: err and posts.
Let's go back to the first listing again. In the onLoad (lines 10 - 14) we have
 if (self.http.status === 200) {
      callback(null, self.http.responseText);
    } else {
      callback('Error: ' + self.http.status);
    }

Open in new window

Let's look at the condition where self.http.status === 200. This will fire the true statement for the if condition which does this
callback(null, self.http.responseText);

Open in new window

This calls the callback [function(err,posts)] with the following values
err => null
posts => self.http.responesText

Open in new window

In the callback we check the err parameter - which in this case will be null - so the else fires and console.log(posts) executes

Let's look at the case were status !== 200
callback('Error: ' + self.http.status);

Open in new window

This is calling our same callback function with
err => 'Error:' + self.http.status
posts => undefined

Open in new window

Going back to the if (err) test in the callback err now has a value and so the true statement of the if fires and console.log(err) executes

So to answer your questions
How come we pass only one argument when we call callback function as below?
In the case of an error - there are no posts so we only pass the error value. We could just as easily have done this
callback('Error: ' + self.http.status, null);

Open in new window

It would have had the same result. In JavaScript we don't have to define parameters if we are not going to use them (PROVIDING they are at the end of the parameter list) and as the callback if statement branches on the err value - where the true part does not use posts - we don't need to included it in the call.

How this function knows that which one is err and which one is posts when only one parameter is passed.
The order of the parameters - as described above. err is always in the first position, posts is always second.
posts is optionally defined but we can also call the callback with posts = null

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IsabellAuthor Commented:
Thanks Julian for your detailed explanation!!!
Julian HansenCommented:
You are welcome.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.