{"action":"created","issue":{"url":"https://api.github.com/repos/philmeehan/promiseAdventure/issues/1","labels_url":"https://api.github.com/repos/philmeehan/promiseAdventure/issues/1/labels{/name}","comments_url":"https://api.github.com/repos/philmeehan/promiseAdventure/issues/1/comments","events_url":"https://api.github.com/repos/philmeehan/promiseAdventure/issues/1/events","html_url":"https://github.com/philmeehan/promiseAdventure/issues/1","id":100248009,"number":1,"title":"Thoughts on chapter 3","user":{"login":"kevinhodges","id":738971,"avatar_url":"https://avatars.githubusercontent.com/u/738971?v=3","gravatar_id":"","url":"https://api.github.com/users/kevinhodges","html_url":"https://github.com/kevinhodges","followers_url":"https://api.github.com/users/kevinhodges/followers","following_url":"https://api.github.com/users/kevinhodges/following{/other_user}","gists_url":"https://api.github.com/users/kevinhodges/gists{/gist_id}","starred_url":"https://api.github.com/users/kevinhodges/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kevinhodges/subscriptions","organizations_url":"https://api.github.com/users/kevinhodges/orgs","repos_url":"https://api.github.com/users/kevinhodges/repos","events_url":"https://api.github.com/users/kevinhodges/events{/privacy}","received_events_url":"https://api.github.com/users/kevinhodges/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-08-11T07:45:51Z","updated_at":"2015-08-11T13:31:21Z","closed_at":null,"body":"I've done  fair bit of playing around with this stuff and you have hit the nail on the head here, scope across promises is an anti-pattern. There's 2 issues I have with the chapter 3 solution.\r\n\r\n1. We can't tell what parameters the functions are expecting nor what data they should return\r\n2. Each function in the chain has the ability to amend any property of `this` at any stage, i'm gonna call this `scope bleed`. If someone were to come in and alter the behaviour of PromiseAdventure._secondFunction, say we make it affect an object that PromiseAdventure._thirdFunction relies on, there is no clear indication that we're breaking the expectations of PromiseAdventure._thirdFunction.\r\n\r\nSomething like the following would remove those issues and firm up/make more visible the contract between functions but gets pretty messy.\r\n\r\n```\r\nvar PromiseAdventure = module.exports = {};\r\n\r\nvar _ = require( 'lodash' );\r\n\r\nPromiseAdventure._orchestrate = function( request ) {\r\n\t// aggregate the responses\r\n\tvar data = {\r\n\t\trequest: request\r\n\t};\r\n\r\n\t// only pass in the variables the function will need\r\n\tvar firstFunctionParams = _.pick( data, 'request' );\r\n\treturn PromiseAdventure._firstFunction( firstFunctionParams ).then( function( newData ) {\r\n\t\treturn _.extend( data, { firstFunctionResponse: newData } );\r\n\t} )\r\n\t.then( function( data ) {\r\n\t\tvar secondFunctionParams = _.pick( data, 'request' );\r\n\t\treturn PromiseAdventure._secondFunction( secondFunctionParams ).then( function( newData ) {\r\n\t\t\treturn _.extend( data, { secondFunctionResponse: newData } );\r\n\t\t} );\r\n\t} )\r\n\t.then( function( data ) {\r\n\t\tvar thirdFunctionParams = _.pick( data, 'property1', 'property2');\r\n\t\treturn PromiseAdventure._thirdFunction( thirdFunctionParams ).then( function( newData ) {\r\n\t\t\treturn _.extend( data, { thirdFunctionResponse: newData } );\r\n\t\t} );\r\n\t} );\r\n};\r\n\r\nPromiseAdventure._firstFunction = function( params ) {\r\n\t// can only see dereferenced \"params.request\" \r\n};\r\nPromiseAdventure._secondFunction = function( params ) {\r\n\t// can only see dereferenced \"params.request\"\r\n};\r\nPromiseAdventure._thirdFunction = function( params ) {\r\n\t// can only see dereferenced \"params.property1\" and \"params.property2\"\r\n};\r\n```"},"comment":{"url":"https://api.github.com/repos/philmeehan/promiseAdventure/issues/comments/129874301","html_url":"https://github.com/philmeehan/promiseAdventure/issues/1#issuecomment-129874301","issue_url":"https://api.github.com/repos/philmeehan/promiseAdventure/issues/1","id":129874301,"user":{"login":"philmeehan","id":46255,"avatar_url":"https://avatars.githubusercontent.com/u/46255?v=3","gravatar_id":"","url":"https://api.github.com/users/philmeehan","html_url":"https://github.com/philmeehan","followers_url":"https://api.github.com/users/philmeehan/followers","following_url":"https://api.github.com/users/philmeehan/following{/other_user}","gists_url":"https://api.github.com/users/philmeehan/gists{/gist_id}","starred_url":"https://api.github.com/users/philmeehan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/philmeehan/subscriptions","organizations_url":"https://api.github.com/users/philmeehan/orgs","repos_url":"https://api.github.com/users/philmeehan/repos","events_url":"https://api.github.com/users/philmeehan/events{/privacy}","received_events_url":"https://api.github.com/users/philmeehan/received_events","type":"User","site_admin":false},"created_at":"2015-08-11T13:31:21Z","updated_at":"2015-08-11T13:31:21Z","body":"That's what we do in the Works currently, and I could code it like that but it becomes harder to test the anonymous functions that you have above."}}