JavaScript Asynchronous Programming with JavaScript Understanding Promises From Callbacks to Promises

gerardo keys
gerardo keys
14,284 Points

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
201,932 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.