{"action":"created","issue":{"url":"https://api.github.com/repos/winjs/winjs/issues/1050","labels_url":"https://api.github.com/repos/winjs/winjs/issues/1050/labels{/name}","comments_url":"https://api.github.com/repos/winjs/winjs/issues/1050/comments","events_url":"https://api.github.com/repos/winjs/winjs/issues/1050/events","html_url":"https://github.com/winjs/winjs/issues/1050","id":68523862,"number":1050,"title":"WinJS.Promise.timeout: How to find out if promise canceled by timeout or user?","user":{"login":"pke","id":164625,"avatar_url":"https://avatars.githubusercontent.com/u/164625?v=3","gravatar_id":"","url":"https://api.github.com/users/pke","html_url":"https://github.com/pke","followers_url":"https://api.github.com/users/pke/followers","following_url":"https://api.github.com/users/pke/following{/other_user}","gists_url":"https://api.github.com/users/pke/gists{/gist_id}","starred_url":"https://api.github.com/users/pke/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pke/subscriptions","organizations_url":"https://api.github.com/users/pke/orgs","repos_url":"https://api.github.com/users/pke/repos","events_url":"https://api.github.com/users/pke/events{/privacy}","received_events_url":"https://api.github.com/users/pke/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/winjs/winjs/labels/.kind:%20question","name":".kind: question","color":"c7def8"}],"state":"open","locked":false,"assignee":{"login":"mikewjackson","id":7026399,"avatar_url":"https://avatars.githubusercontent.com/u/7026399?v=3","gravatar_id":"","url":"https://api.github.com/users/mikewjackson","html_url":"https://github.com/mikewjackson","followers_url":"https://api.github.com/users/mikewjackson/followers","following_url":"https://api.github.com/users/mikewjackson/following{/other_user}","gists_url":"https://api.github.com/users/mikewjackson/gists{/gist_id}","starred_url":"https://api.github.com/users/mikewjackson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mikewjackson/subscriptions","organizations_url":"https://api.github.com/users/mikewjackson/orgs","repos_url":"https://api.github.com/users/mikewjackson/repos","events_url":"https://api.github.com/users/mikewjackson/events{/privacy}","received_events_url":"https://api.github.com/users/mikewjackson/received_events","type":"User","site_admin":false},"milestone":null,"comments":2,"created_at":"2015-04-14T23:15:50Z","updated_at":"2015-04-20T12:34:37Z","closed_at":null,"body":"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. However, there is no way to find out that the promise has been cancelled by the user or by the timeout.\r\nIt's too late in the game but the timeout promise should probably cancel itself with a different error than a call to `cancel()` would normally produce, something like \"timeout\". Then we could check in our error handler:\r\n```\r\npendingRequest.then(null, function(error) {\r\n  if (error.name === \"Canceled\") // by user\r\n  if (error.name === \"Timeout\") // by timeout\r\n}); \r\n```\r\n\r\nOr the cancel method would optionally accept an `error` argument to use to set the promise into error state.\r\n\r\nAny idea how to do that with the current state of Promises?"},"comment":{"url":"https://api.github.com/repos/winjs/winjs/issues/comments/94440893","html_url":"https://github.com/winjs/winjs/issues/1050#issuecomment-94440893","issue_url":"https://api.github.com/repos/winjs/winjs/issues/1050","id":94440893,"user":{"login":"pke","id":164625,"avatar_url":"https://avatars.githubusercontent.com/u/164625?v=3","gravatar_id":"","url":"https://api.github.com/users/pke","html_url":"https://github.com/pke","followers_url":"https://api.github.com/users/pke/followers","following_url":"https://api.github.com/users/pke/following{/other_user}","gists_url":"https://api.github.com/users/pke/gists{/gist_id}","starred_url":"https://api.github.com/users/pke/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pke/subscriptions","organizations_url":"https://api.github.com/users/pke/orgs","repos_url":"https://api.github.com/users/pke/repos","events_url":"https://api.github.com/users/pke/events{/privacy}","received_events_url":"https://api.github.com/users/pke/received_events","type":"User","site_admin":false},"created_at":"2015-04-20T12:34:37Z","updated_at":"2015-04-20T12:34:37Z","body":"Another proposal: \r\n\r\n```\r\n(function (P) {\r\n        var oldTimeout = P.timeout\r\n        P.timeout = function (t, promise) {\r\n            var timeoutPromise = oldTimeout(t);\r\n            if (promise) {\r\n                return new WinJS.Promise(function (c, e, p) {\r\n                    promise.then(c,e,p);\r\n                    timeoutPromise.then(function () {\r\n                        e(new WinJS.ErrorFromName(\"Timeout\", \"Timeout reached after \" + t + \"ms\"));\r\n                    });\r\n                });\r\n            } else {\r\n                return timeoutPromise;\r\n            }\r\n        };\r\n    })(WinJS.Promise);\r\n```\r\n\r\nThis one does not cancel the timeout promise when the real promise is fullfilled/rejected.\r\n\r\nWhich version you guys think is the better one? I tend to favour the first one, as cancellation of the timeout promise saves a few CPU cycles and resources."}}