{"action":"created","issue":{"url":"https://api.github.com/repos/petkaantonov/bluebird/issues/489","labels_url":"https://api.github.com/repos/petkaantonov/bluebird/issues/489/labels{/name}","comments_url":"https://api.github.com/repos/petkaantonov/bluebird/issues/489/comments","events_url":"https://api.github.com/repos/petkaantonov/bluebird/issues/489/events","html_url":"https://github.com/petkaantonov/bluebird/issues/489","id":57230113,"number":489,"title":"Problem with Join","user":{"login":"carlskii","id":555399,"avatar_url":"https://avatars.githubusercontent.com/u/555399?v=3","gravatar_id":"","url":"https://api.github.com/users/carlskii","html_url":"https://github.com/carlskii","followers_url":"https://api.github.com/users/carlskii/followers","following_url":"https://api.github.com/users/carlskii/following{/other_user}","gists_url":"https://api.github.com/users/carlskii/gists{/gist_id}","starred_url":"https://api.github.com/users/carlskii/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/carlskii/subscriptions","organizations_url":"https://api.github.com/users/carlskii/orgs","repos_url":"https://api.github.com/users/carlskii/repos","events_url":"https://api.github.com/users/carlskii/events{/privacy}","received_events_url":"https://api.github.com/users/carlskii/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-02-10T20:15:55Z","updated_at":"2015-02-10T20:30:49Z","closed_at":"2015-02-10T20:30:49Z","body":"I seem to having problems using `Promise.join`.  I have the following code:\r\n\r\n```javascript\r\nvar Promise = require(\"bluebird\");\r\nvar Request = require('request');\r\n\r\nfunction callhttp(host, cb) {\r\n\r\n    var cert = \" \";\r\n\r\n    var options = {\r\n        url: 'https://' + host\r\n    };\r\n\r\n    function callback(error, response, body) {\r\n        if (!error && response.statusCode == 200) {\r\n            var cert = response.connection.getPeerCertificate().fingerprint;\r\n            cb(cert);\r\n        }\r\n    }\r\n    Request(options, callback);\r\n}\r\n\r\ncallhttp(\"www.google.com\", function(_cert) {\r\n  //  console.log(_cert);\r\n});\r\n\r\nvar join = Promise.join;\r\n    join(callhttp(\"www.google.com\", function(_cert) {\r\n        console.log(_cert);\r\n    }),\r\n    callhttp(\"www.microsoft.com\", function(_cert) {\r\n        console.log(_cert);\r\n    }),\r\n    callhttp(\"www.adobe.com\", function(_cert) {\r\n        console.log(_cert);\r\n    }),\r\n    function(first, second, third) {\r\n        console.log({fp1: first, fp2: second, fp3: third});\r\n    });\r\n```\r\n\r\nHowever the join does not seem to work as documented and outputs this:\r\n```\r\n{ fp1: undefined, fp2: undefined, fp3: undefined }\r\nE9:E2:F5:B9:CD:DA:73:CD:41:94:20:EA:51:88:4D:F0:FC:11:6F:4E\r\n94:C1:5B:66:01:00:B0:23:E0:C7:DA:A9:AC:47:3F:BC:3B:E1:C0:53\r\n09:BF:C6:28:E9:E4:94:A7:2B:CD:EE:2B:86:AB:44:A3:72:39:28:C2\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/petkaantonov/bluebird/issues/comments/73777679","html_url":"https://github.com/petkaantonov/bluebird/issues/489#issuecomment-73777679","issue_url":"https://api.github.com/repos/petkaantonov/bluebird/issues/489","id":73777679,"user":{"login":"petkaantonov","id":829734,"avatar_url":"https://avatars.githubusercontent.com/u/829734?v=3","gravatar_id":"","url":"https://api.github.com/users/petkaantonov","html_url":"https://github.com/petkaantonov","followers_url":"https://api.github.com/users/petkaantonov/followers","following_url":"https://api.github.com/users/petkaantonov/following{/other_user}","gists_url":"https://api.github.com/users/petkaantonov/gists{/gist_id}","starred_url":"https://api.github.com/users/petkaantonov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/petkaantonov/subscriptions","organizations_url":"https://api.github.com/users/petkaantonov/orgs","repos_url":"https://api.github.com/users/petkaantonov/repos","events_url":"https://api.github.com/users/petkaantonov/events{/privacy}","received_events_url":"https://api.github.com/users/petkaantonov/received_events","type":"User","site_admin":false},"created_at":"2015-02-10T20:30:49Z","updated_at":"2015-02-10T20:30:49Z","body":"There is a couple of basics that are wrong, I am actually working on a beginner's guide so stay tuned.\r\n\r\nIn the meanwhile...\r\n\r\nYou never want to use callbacks manually anymore, as wiring callbacks manually to promises effectively results in code that has all the problems of callbacks with some additional verbosity.\r\n\r\nSo first lesson is to promisify at the lowest level:\r\n\r\n```js\r\nvar Promise = require(\"bluebird\");\r\nvar Request = Promise.promisify(require('request'));\r\n```\r\n\r\nNow `Request` is a function that returns a promise (and doesn't take a callback anymore).\r\n\r\nSecondly, `callhttp` is not returning a promise - promises are wired together with the `return` statement. It also should not take any callback since it's using promises now. \r\n\r\n```js\r\nfunction callhttp(host) {\r\n\r\n    var cert = \" \";\r\n\r\n    var options = {\r\n        url: 'https://' + host\r\n    };\r\n\r\n    return Request(options).spread(function(response, body) {\r\n        if (response.statusCode == 200) {\r\n            return response.connection.getPeerCertificate().fingerprint;\r\n        } else {\r\n            // Just an example, 200 is not the only successful code\r\n            throw new Error(\"HTTP Error: \" + response.statusCode);\r\n        }\r\n    });\r\n}\r\n```\r\n\r\nAs for join, the signature is `promises..., handler` so:\r\n\r\n```js\r\nvar join = Promise.join;\r\njoin(\r\n    callhttp(\"www.google.com\"),\r\n    callhttp(\"www.microsoft.com\"),\r\n    callhttp(\"www.adobe.com\"),\r\n    function(first, second, third) {\r\n    console.log({fp1: first, fp2: second, fp3: third});\r\n });\r\n ```"}}