4

I'm new to promises, but I've looked at some examples using Deft.js and I'm wondering why my basic example isn't working.

Looking at This site, I thought the .done() function should work, but I keep getting errors.

Here is my code:

onScopeChange: function(scope) {
    var promise = this.loadStories(scope).then({
        success: function(records) {
            console.log(records);
        },
        failure: function(error) {
            alert(error);
        }
    }).always(function() {
        // Do something whether call succeeded or failed
        console.log('this should always appear');
    }).done();
},

loadStories: function(scope) {
    var deferred = Ext.create('Deft.promise.Deferred');

    Ext.create('Rally.data.wsapi.Store', {
        autoLoad: true,
        model: 'UserStory',
        fetch: ['FormattedID', 'Name', 'ScheduleState','RevisionHistory'],
        filters: [scope.getQueryFilter()],
        listeners: {
            load: function(me, records, success) {
                this._onStoriesLoaded(deferred, records, success);
            },
            scope: this
        }
    });

    return deferred.promise;
},

_onStoriesLoaded: function(deferred, records, success) {
    console.log('stories loaded ...');
    if (success) {
        deferred.resolve(records);
    } else {
        deferred.reject("Error loading stories");
    }
}

This is just a very basic example where 'Rally.data.wsapi.Store' leads to asynchronous callbacks.

My question is only why the .done() does not work - I keep getting an error saying:

TypeError: Object [object Object] has no method 'done'

Which implies that my 'promise' object is not actually a Deft Promise object, right?

Blundering Philosopher
  • 6,245
  • 2
  • 43
  • 59

1 Answers1

2

Are you sure you're using the last version of Deft? On the page you're linking, he says done() will be integrated into Deft v0.9.

I've tested the following code against Deft v0.9.1, and it's working exactly as intended:

function defer() {
    var deferred = new Deft.promise.Deferred;

    deferred.reject('Done');

    return deferred.promise;
}

defer()
    .then({
        success: function(result) {
            console.log('Success: ' + result);
        }
        ,failure: function(result) {
            console.log('Failure: ' + result);
            a = b; // runtime error
        }
    })
    .always(function() {
        console.log('Always');
    })
    .done();

Gives me:

Failure: Done app.js:15
Always app.js:20
Uncaught ReferenceError: b is not defined 

I'm not told done() is undefined, and without the done() call, the runtime error is swallowed.

(By the way, thanks for bringing to my attention that Deft has evolved... The muting of errors has been something that once gave me some great sorrows!)

rixo
  • 23,815
  • 4
  • 63
  • 68