+1 for this approach with the note that theres probably one or more 
EventEmitters already in scope that you could leverage.  Creating new 
emitters and or registering event handlers within callbacks or other event 
handlers can lead to unexpected leaks.   Worth remembering that the global 
"process" is an EventEmitter, so for simple programs it's perfectly 
acceptable to write

process.on("whatever", function (error, data) { 

    ... 

});


I wouldn't do this from within a module since it can cause non-obvious 
behavior, but in the main program file it's fine.  In a web server context 
you generally have access an http.Server which is EventEmitter as well, as 
well as request and response objects.

On Monday, September 15, 2014 12:10:01 PM UTC-7, Edouard Moon wrote:
>
> Hi,
>
> I'm more onto events with EventEmitter() when I need to do a lot of 
> async/sync stuff.
> It's built in nodejs, and easy to use out of the box, npm is not required.
>
> Example with sync stuff :
>
> function (data, callback) {
>   var Events = require("events");
>   var event = new Events.EventEmitter();
>
>   event.on("job1", function (data) {
>     // do stuff with data
>     event.emit("job2", err, data);
>   });
>
>   event.on("job2", function (err, data) {
>     // do stuff with err and data
>     event.emit("done", err, data);
>   });
>
>   event.on("done", function (err, data) {
>     // do stuff with err and data
>     // delete event and remove listeners
>     callback(err, data);
>   });
>  
>   event.emit("job1", data);
> }
>
> You get the idea.
> For async just call emit after a function is done and keep an array 
> updated so the "done" handle know when all the functions are executed.
>
> Regards.
>
> Edouard Buschini
>  
>  
> 15.09.2014, 21:13, "// ravi" <ravi-...@g8o.net <javascript:>>:
>
> On Sep 15, 2014, at 1:01 PM, Alex Kocharin <al...@kocharin.ru 
> <javascript:>> wrote:
>
>  
> Promises just wrap callbacks. So you had callback hell, now you have 
> wrapped callback hell. Nice change, huh.
>
>  
>  
> Actually yes, I think it is. Readable code is worth it, in my view.
>
>  
>  
> Promises are really useful for a couple of things like error handling, but 
> readability isn't one of them.
>  
> Quite the opposite, that ".then(...)" noise is even worse than callbacks. 
> Try to rewrite that example with promises, I'm sure it won't be more 
> readable.
>  
> I think generators are the only clean answer here. I know they aren't even 
> in stable node, but still, they're worth recommending. Well there are 
> fibers also, but they seem to be forgotten already.
>  
>  
>
>  
>  
>
> If you want to turn async code to sync-like code, I would suggest to use 
> generators and `co` module instead.
>
>  
>  
> I am waiting for the right time to start recommending generators+X to 
> folks asking these sort of questions. I am not comfortable yet doing so, 
> but I agree that anyone interested should take a look.
>  
> —ravi
>  
>  
>
>  
> -- 
> Job board: http://jobs.nodejs.org/
> New group rules: 
> https://gist.github.com/othiym23/9886289#file-moderation-policy-md
> Old group rules: 
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> --- 
> You received this message because you are subscribed to the Google Groups 
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to nodejs+un...@googlegroups.com <javascript:>.
> To post to this group, send email to nod...@googlegroups.com <javascript:>
> .
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/nodejs/6BCCC31B-7FCD-4B20-A045-7FFAC897A53A%40g8o.net
>  
> <https://groups.google.com/d/msgid/nodejs/6BCCC31B-7FCD-4B20-A045-7FFAC897A53A%40g8o.net?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
>  -- 
> Job board: http://jobs.nodejs.org/
> New group rules: 
> https://gist.github.com/othiym23/9886289#file-moderation-policy-md
> Old group rules: 
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> --- 
> You received this message because you are subscribed to the Google Groups 
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to nodejs+un...@googlegroups.com <javascript:>.
> To post to this group, send email to nod...@googlegroups.com <javascript:>
> .
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/nodejs/500601410802189%40web21h.yandex.ru 
> <https://groups.google.com/d/msgid/nodejs/500601410802189%40web21h.yandex.ru?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>  

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
To post to this group, send email to nodejs@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/090b4545-bf91-4273-99ee-7b7b7b419207%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to