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 Asynchronous Programming with JavaScript Understanding Promises From Callbacks to Promises

I used jQuery...

I used jQuery for the getJSON function under the Promise like this:

// Make an AJAX request
function getJSON(url) {
  return new Promise((resolve, reject) =>{
    $.ajax({
      type: "GET",
      url: url,
      dataType: "JSON",
      success: function (response) {
        resolve(response);
      },
      error: function (xhr) {
        reject(Error(xhr.statusText));
        xhr.on('error', () => {
          reject(Error('A network error occurred'));
        })
      }
    }); //end ajax
  });// end promise
};

It worked just like the vanilla javascript way but definitely easier for me to understand. Would there be anything wrong or unknown errors occurring by using jQuery?

1 Answer

Steven Parker
Steven Parker
229,744 Points

This is probably more typical of a real-life implementation. I think the primitive functions are rarely used in actual development. I expect most AJAX is done either with jQuery or the "fetch" method.