1

The Problem

I am trying to return the result of jQuery's getJSON method.

What I've Tried

I under stand that the getJSON method is asynchronous, and that I cannot simply return the result as you can with synchronous code.

I've looked at other posts and I tried messing around with promises, as well as callbacks, etc... but have gotten nowhere.

My Code (Before Trying Promises, Callbacks, Etc...)

function getCart() {
  jQuery.getJSON("/cart.js").done(function (cart) {
      return cart;
  });
}

cart = getCart();
console.log(cart);

Any direction would be greatly appreciated!

Brandon
  • 155
  • 10
  • 1
    Why don't you execute your code after the promise is resolved? Just return the promise from the function `return jQuery.getJSON("/cart.js");` and then `getCart().done(function(cart) { console.log(cart) });` – Hanlet Escaño Mar 04 '21 at 18:44

0 Answers0