{"action":"created","issue":{"url":"https://api.github.com/repos/kriskowal/q/issues/699","labels_url":"https://api.github.com/repos/kriskowal/q/issues/699/labels{/name}","comments_url":"https://api.github.com/repos/kriskowal/q/issues/699/comments","events_url":"https://api.github.com/repos/kriskowal/q/issues/699/events","html_url":"https://github.com/kriskowal/q/issues/699","id":83430880,"number":699,"title":"Trouble with chained promises","user":{"login":"AliUz","id":2784712,"avatar_url":"https://avatars.githubusercontent.com/u/2784712?v=3","gravatar_id":"","url":"https://api.github.com/users/AliUz","html_url":"https://github.com/AliUz","followers_url":"https://api.github.com/users/AliUz/followers","following_url":"https://api.github.com/users/AliUz/following{/other_user}","gists_url":"https://api.github.com/users/AliUz/gists{/gist_id}","starred_url":"https://api.github.com/users/AliUz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AliUz/subscriptions","organizations_url":"https://api.github.com/users/AliUz/orgs","repos_url":"https://api.github.com/users/AliUz/repos","events_url":"https://api.github.com/users/AliUz/events{/privacy}","received_events_url":"https://api.github.com/users/AliUz/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-06-01T10:41:51Z","updated_at":"2015-06-03T15:26:09Z","closed_at":null,"body":"I am using Express and in one of my get routes I have a promise structure, which basically builds an object and finally returns it to the user via res.send. \r\nAt high loads (thousands of requests per second), I start to see exceptions being thrown in my application. Here is how my application is structured:\r\n\r\n```javascript\r\n            app.get('/endpoint', function (req, res) {\r\n            validator.validate(req.query, {\r\n                // check query to see if valid\r\n            }).then(function (valResult) {\r\n               // if result passes validation get application id from database\r\n            }).then(function (app) {\r\n            // do a bunch of IO operations to build resulting object\r\n            })\r\n            //processObj functions return deferred.promises \r\n            .then(function(obj1){ return processObj1(obj1); }) \r\n            .then(function(obj2){ return processObj2(obj2); })\r\n            .then(function(obj3){ return processObj3(obj3); })\r\n            }).then(function (result) {\r\n                if (result.platform === 'mobile') {\r\n                    res.jsonp({code: 100, message: 'SUCCESS', data: result});\r\n                }\r\n                else {\r\n                    res.send({code: 100, message: 'SUCCESS', data: result});\r\n                }\r\n            }).fail(function (error) {\r\n                if (error.stack) {\r\n                    console.log(error.message);\r\n                    console.log(error.stack);\r\n                    res.send({code: 500, message: 'FAIL_SYSTEM', data: error.message});\r\n                } else {\r\n                    res.send(error);\r\n                }\r\n            });\r\n        }\r\n```\r\n\r\nI am getting an Error saying that the application cannot set headers after they are sent which is an Express error telling me that I have already issued a res.send so I can't issue it again. \r\nIs it possible that this error occurs because my promises are not executing in order? I tried to debug the application and saw that the middle \"then\"s execute after the final \"then\", is this even possible using promises? Or could it be that one of the promises is returning more than one promise to the next function in the chain? \r\n\r\nHere is the full error (using q@0.9.7):\r\n\r\n``` javascript\r\nError: Can't set headers after they are sent.\r\n    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11)\r\n    at ServerResponse.res.setHeader (/node_modules/express/node_modules/connect/lib/patch.js:59:22)\r\n    at ServerResponse.res.set.res.header (/node_modules/express/lib/response.js:522:10)\r\n    at ServerResponse.res.jsonp (/node_modules/express/lib/response.js:236:8)\r\n    at <path-to-module>/module.js:157:29\r\n    at _fulfilled (/node_modules/q/q.js:798:54)\r\n    at self.promiseDispatch.done (/node_modules/q/q.js:827:30)\r\n    at Promise.promise.promiseDispatch (/node_modules/q/q.js:760:13)\r\n    at /node_modules/q/q.js:574:44\r\n    at flush (/node_modules/q/q.js:108:17)\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/kriskowal/q/issues/comments/108482394","html_url":"https://github.com/kriskowal/q/issues/699#issuecomment-108482394","issue_url":"https://api.github.com/repos/kriskowal/q/issues/699","id":108482394,"user":{"login":"AliUz","id":2784712,"avatar_url":"https://avatars.githubusercontent.com/u/2784712?v=3","gravatar_id":"","url":"https://api.github.com/users/AliUz","html_url":"https://github.com/AliUz","followers_url":"https://api.github.com/users/AliUz/followers","following_url":"https://api.github.com/users/AliUz/following{/other_user}","gists_url":"https://api.github.com/users/AliUz/gists{/gist_id}","starred_url":"https://api.github.com/users/AliUz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AliUz/subscriptions","organizations_url":"https://api.github.com/users/AliUz/orgs","repos_url":"https://api.github.com/users/AliUz/repos","events_url":"https://api.github.com/users/AliUz/events{/privacy}","received_events_url":"https://api.github.com/users/AliUz/received_events","type":"User","site_admin":false},"created_at":"2015-06-03T15:26:09Z","updated_at":"2015-06-03T15:26:09Z","body":"Nevermind, my issue has nothing to do with the promise structure, the error is a result of express timing out then the promise trying to resolve and attempting to do a res.send after the timeout response causing this error. "}}