Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Handling Errors

Rebecca Palumbo
Rebecca Palumbo
5,506 Points

How is it that you can call a function into a then handler without an argument or parentheses?

But then you put an argument in the function? I'm confused. I'm referring to the checkStatus function here.

2 Answers

Steven Parker
Steven Parker
231,271 Points

When you set up a handler, one of the arguments is a function. But you don't want to call the function at that point, you only want to pass a reference to the function so the handler can call it when the event occurs.

That's why when you use a named function as a callback argument you don't put parentheses after the name.

Steven Parker
Steven Parker
231,271 Points

A "then" always passes the fulfillment value (that it gets from the promise it is attached to) along to handler function. So even though you only give the function's name to "then", when the function is actually called, it gets an argument.

Rebecca Palumbo
Rebecca Palumbo
5,506 Points

I don't understand. At the end of the video, there is .then(checkStatus) which calls the checkStatus function that has a response argument. How does this work? The .then handler with the call for checkStatus doesn't have an argument. How does the response info get transferred to the function? Would any function have response info transferred with it?

Alex Hort-Francis
Alex Hort-Francis
17,074 Points

So bear in mind that we're 'chaining' .then() methods together, one after the other.

Doing so means that the return value from one chained promise is automatically fed through into the next promise, and so on.

More info here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises#chaining

And here: https://www.javascripttutorial.net/es6/promise-chaining/

Because the then() method returns a new Promise with a value resolved to a value, you can call the then() method on the return Promise...

In this example, the return value in the first then() method is passed to the second then() method. You can keep calling the then() method successively...