0

I have an asynchronous ajax function, complete with a callback:

var coreChat = {

      fetch: function (fn) {
        $.ajax({
          url: "https://api.parse.com/1/classes/chats",
          data: {
             order: 'createdAt',
             limit: 10
          },
          type: "GET"
        })
        .done(function(data) {
          var messages = [];
          for (var i = 0, len = data.results.length; i < len; i++) {
            messages.push(data.results[i].text);
          }

          return fn(messages);
        });
      },

I'm pretty sure the .done(function(data) {... portion of the above code is the callback, though feel free to correct me if I'm wrong.

Now I'm getting confused when this fetch asynchronous function is called later on in the code...

    var myChat = {

    updateMessages: function () {
        this.fetch(function (messages) {
          myChat.display(messages);
        });
      }

    };

    $.extend(myChat, coreChat);
    myChat.init();

In the updateMessages function, fetch is called with a parameter, which is another function - my question is - is that function the new callback? Does it replace the callback that was originally defined in the fetch function? Or maybe it's the argument to the callback that's originally called in the fetch function? I'm a bit lost here and any help would be appreciated...

If you'd like a link to the entire code:

all the code

dhana
  • 6,487
  • 4
  • 40
  • 63
Seeeyonnn
  • 315
  • 2
  • 10
  • 19
  • No; it's just a function that your (inner) callback calls. Callbacks are not magic; they're just regular functions that you can call whenever you like. – SLaks Feb 02 '14 at 01:56
  • so it's a callback to the callback? @SLaks – Seeeyonnn Feb 02 '14 at 02:00

0 Answers0