I'm writing a function to load events (for a calendar) which should return data to be parsed by Mustache.
The problem i got is that I can't find a way to return processed data inside request.done()

Here's my function

```
this.load = function() {
    var events = createArray(7, { content: [] });
    var data = {
        from: Math.floor(this.week[0].valueOf() / 1000),
        to: Math.floor(this.week[6].valueOf() / 1000)
    };
    var request = $.ajax({
        url: this.loadPath,
        data: data,
        type: 'POST'
    });

    request.done(function(response) {
        response = JSON.parse(response);
        response.forEach(function(event) {
            event.date = new Date(event.date);
            var thisEvent = {
                id: event.id,
                time: event.date.format('HH:MM'),
                title: event.title,
                description: event.description,
                color: event.color,
                link: event.link
            };

            // Why is thisEvent added to each events[].content and not just for events[day].content?
            events[event.date.getRealDay()].content.push(thisEvent);
        });
    });

    // return statement has to be executed only when request.done() has finished
    // or find a way to return directly from request.done()
    return { events: events };
};

```

Thank you for your help

EDIT: Solution found

```
this.load = function() {
    var data = {
        from: Math.floor(this.week[0].valueOf() / 1000),
        to: Math.floor(this.week[6].valueOf() / 1000)
    };
    var request = $.ajax({
        url: this.loadPath,
        data: data,
        type: 'POST',
        context: this
    });
    var deferred = $.Deferred();

    request.done(function(response) {
        var events = createArray(7, { content: [] });
        response = JSON.parse(response);

        response.forEach(function(event) {
            event.date = new Date(event.date);
            var thisEvent = { // Why does the
                id: event.id,
                time: event.date.format('HH:MM'),
                title: event.title,
                description: event.description,
                color: event.color,
                link: event.link
            };
            events[event.date.getRealDay()].content.push(thisEvent); // Why is thisEvent added to each events[].content and not just for events[day].content?
        });
        deferred.resolveWith(this, events);
    });
    request.fail(deferred.reject);

    return deferred.promise();
};

```

From another function I can call :

```
this.load().done(function(events) {
    this.events = events;
    this.render();
});

```