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 AJAX Basics AJAX Concepts How AJAX Works

Routine Poutine
Routine Poutine
26,050 Points

AJAX: Are callback functions before or after opening the stream?

Hi, Is it better to put the callback function before opening the stream? (I'm not sure if you call this a stream or connection):

I thought the order of AJAX was:

I. Create New XHR Object II. Open Connection III. Event Handler (Callback) IV. Send

const xhr = new XMLHttpRequest();

xhr.open('GET', url, true);

xhr.onload = function() { }

xhr.send()

Why is it better to put the callback second?

Best, Matt

1 Answer

Pepe ToΓ±o
Pepe ToΓ±o
13,815 Points

Hi! As long as you set the 'callback' before the 'send' method is correct, undistinctly if you put it at first or second place on your declaration right before the 'send' method.