1

for some reason i can't seem to setup a deferred. Here s my setup

// some class
find: function () {
    var deferred = $.Deferred();

    Func.run(function (err, results) {
        return results;
    });

    deferred.resolve(results);

    return deferred.promise;
},
test: function () {
    $.when(this.find()).done(function(data){
        console.log(data);
    });
}

im trying to call this.find() but i want it to return a promise... the issue is that Func.run() is async, so i need to wait until that is finished as well.

I'm also using backbone.js, is there is a different way to do it there.

any ideas what is wrong with my code?

Patrioticcow
  • 26,422
  • 75
  • 217
  • 337

1 Answers1

0

Put your resolve statement inside your Func.run:

find: function () {
    var deferred = $.Deferred();

    clearTimeout(this.timer);
    this.timer = setTimeout(function () {
        return Func.run(function (err, results) {
            deferred.resolve(results);
        });
    }, 1500);

    return deferred.promise();
}
dashtinejad
  • 6,193
  • 4
  • 28
  • 44