{"action":"created","issue":{"url":"https://api.github.com/repos/adleroliveira/dreamjs/issues/10","labels_url":"https://api.github.com/repos/adleroliveira/dreamjs/issues/10/labels{/name}","comments_url":"https://api.github.com/repos/adleroliveira/dreamjs/issues/10/comments","events_url":"https://api.github.com/repos/adleroliveira/dreamjs/issues/10/events","html_url":"https://github.com/adleroliveira/dreamjs/issues/10","id":121820499,"number":10,"title":"callback is called synchronously","user":{"login":"yortus","id":1252361,"avatar_url":"https://avatars.githubusercontent.com/u/1252361?v=3","gravatar_id":"","url":"https://api.github.com/users/yortus","html_url":"https://github.com/yortus","followers_url":"https://api.github.com/users/yortus/followers","following_url":"https://api.github.com/users/yortus/following{/other_user}","gists_url":"https://api.github.com/users/yortus/gists{/gist_id}","starred_url":"https://api.github.com/users/yortus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yortus/subscriptions","organizations_url":"https://api.github.com/users/yortus/orgs","repos_url":"https://api.github.com/users/yortus/repos","events_url":"https://api.github.com/users/yortus/events{/privacy}","received_events_url":"https://api.github.com/users/yortus/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-12-12T00:43:21Z","updated_at":"2015-12-15T02:17:30Z","closed_at":null,"body":"Hi @adleroliveira,\r\n\r\nSaw this at top of /r/node, congrats!\r\n\r\nI noticed that dreamjs supports an async API but its implementation is fully synchronous. This will lead to errors in usage like the following:\r\n```javascript\r\ndream.output((err, result) => {\r\n  useResult(result);\r\n});\r\nvar useResult = () => {\r\n    // do something with result...\r\n}\r\n```\r\n\r\nThe async callback model of node guarantees that the callback won't be called until the next turn of the event loop (at the earliest), so the example *should be* 100% safe.\r\n\r\nIt looks like your implementation does everything synchronously and calls the callback synchronously too, so the `useResult(result);` line gets called before `useResult` is defined, and an error ensues.\r\n\r\nMore generally, there's no point offering callbacks/Promises/streams if the implementation is synchronous. The synchronous implementation completely starves the event loop until it finishes, so there is no opportunity for other code to run. I would suggest:\r\n* Either: drop the asynchronous API since it's a fully synchronous implementation\r\n* Or: change the implementation to break processing into smaller parts which are processed asynchronously by putting them individually on the event loop using eg `setImmediate`. When the full output is ready, *then* return the result through the callback/Promise/etc.\r\n\r\nAs for which is best, I'd say it depends how much processing is likely to be involved in producing deamjs outputs. If it's no more than a millisecond or two in the worst case, you might as well just keep it synchronous.\r\n\r\nOn the other hand if you get people wanting to generate MBs of test data from this, that could take a while to generate. Your current implementation will starve the event loop even though the API 'looks' asychronous. In this case you should definitely break up the work as suggested above.\r\n\r\nAgain, nice lib, and that's just my 2c!\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/adleroliveira/dreamjs/issues/comments/164621577","html_url":"https://github.com/adleroliveira/dreamjs/issues/10#issuecomment-164621577","issue_url":"https://api.github.com/repos/adleroliveira/dreamjs/issues/10","id":164621577,"user":{"login":"adleroliveira","id":713256,"avatar_url":"https://avatars.githubusercontent.com/u/713256?v=3","gravatar_id":"","url":"https://api.github.com/users/adleroliveira","html_url":"https://github.com/adleroliveira","followers_url":"https://api.github.com/users/adleroliveira/followers","following_url":"https://api.github.com/users/adleroliveira/following{/other_user}","gists_url":"https://api.github.com/users/adleroliveira/gists{/gist_id}","starred_url":"https://api.github.com/users/adleroliveira/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adleroliveira/subscriptions","organizations_url":"https://api.github.com/users/adleroliveira/orgs","repos_url":"https://api.github.com/users/adleroliveira/repos","events_url":"https://api.github.com/users/adleroliveira/events{/privacy}","received_events_url":"https://api.github.com/users/adleroliveira/received_events","type":"User","site_admin":false},"created_at":"2015-12-15T02:17:30Z","updated_at":"2015-12-15T02:17:30Z","body":"Thanks a lot for your comment.\r\n\r\nYou are totally right and I am already working on a version with true async resolution of the custom types (witch will bring a lot more power to the library).\r\n\r\nI will be soon creating a brach with a draft of the new verion and you are more than welcome to check it out or contribute to it.\r\n\r\nI appreciate your input.\r\n\r\nCheers"}}