RSW
RSW

Reputation: 1667

Unable to get property 'done' of undefined or null reference

Why do I get the error message "Unable to get property 'done' of undefined or null reference" when I run the following code?

new WinJS.Promise(initFunc).then(function () {
                       /* do something that returns a promise */
}).done(function () {
                       /* do something that returns a promise */
}).then(function () {
                       /* do something that returns a promise */
}).done(function () {
});

Upvotes: 1

Views: 3228

Answers (2)

RSW
RSW

Reputation: 1667

You can only call done() once in a promise chain, and that must be at the end of the chain. In the problematic code, the done() function is called twice in the promise chain:

new WinJS.Promise(initFunc).then(function () {
}).done(function () {     <====== done() is incorrectly called here--should be then()
}).then(function () {     <====== the call to then() here will throw an error
}).done(function () {             
});

This problem scenario can happen when your code starts off with two separate promise chains and you end up combining them together at some later point, as follows:

new WinJS.Promise(initFunc).then(function () {
                       /* do something that returns a promise */
}).done(function () {     <====== change this done() to a then() if you combine the two
});                               promise chains

new WinJS.Promise(initFunc).then(function () {
                       /* do something that returns a promise */
}).done(function () {
});

Upvotes: 1

siger
siger

Reputation: 3162

You would get the same error simply on:

new WinJS.Promise(initFunc).then(function () {
}).done(function () {
});

because your code in then didn't return a promise on which to call done on.

new WinJS.Promise(initFunc).then(function () {
    // Return a promise in here
    // Ex.:
    return WinJS.Promise.timeout(1000);
}).done(function () {
});

And to come back to your initial code, as you mentioned in your answer you shouldn't chain multiple done together.

Upvotes: 0

Related Questions