lundi 20 avril 2015

How to find out if WinJS.Promise was cancelled by timeout or cancel() call

I have a server request that is wrapped in a timeout promise.

var pendingRequest = WinJS.Promise.timeout(5000, requestAsync).

The user also has a "Cancel" button on the UI to actively cancel the request by executing pendingRequest.cancel(). However, there is no way to find out that the promise has been cancelled by the user or by the timeout (since timeout calls promise.cancel() internally too).

It would have been nice of WinJS.Promise.timeout would move the promise in the error state with a different Error object like "Timeout" instead of "Canceled".

Any idea how to find out if the request has been cancelled by the timeout?

Update: How about this solution:

(function (P) {
        var oldTimeout = P.timeout
        P.timeout = function (t, promise) {
            var timeoutPromise = oldTimeout(t);
            if (promise) {
                return new WinJS.Promise(function (c, e, p) {
                    promise.then(c,e,p);
                    timeoutPromise.then(function () {
                        e(new WinJS.ErrorFromName("Timeout", "Timeout reached after " + t + "ms"));
                    });
                });
            } else {
                return timeoutPromise;
            }
        };
    })(WinJS.Promise);

Aucun commentaire:

Enregistrer un commentaire